﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Collections;
using System.Threading;
using System.IO;

namespace LED_Tester
{
    public partial class frmMain : Form
    {
        #region 构造函数

        public frmMain()
        {
            InitializeComponent();
        }

        #endregion

        #region 电源变量

        private ClassSourceMeter classSourceMeter = new ClassSourceMeter();
        private int sourceMode;
        private SerialPort sptSource = new SerialPort();
        private string sourcePortName;
        private bool sourceIsJoin;

        private double voltSet;
        private double currSet;
        private double voltCheck;
        private double currCheck;

        private int preheatTime;

        #endregion

        #region 控制卡变量

        private bool ctrlIsJoin;

        private const int XCH = 0;
        private const int YCH = 1;
        private const int ZCH = 2;

        private int strVelXCH = 100;
        private int maxVelXCH = 1000;
        private int strVelYCH = 100;
        private int maxVelYCH = 1000;
        private int strVelZCH = 500;
        private int maxVelZCH = 5000;

        private double tacc = 0.1;

        private int offsetXCH = 0;
        private int offsetYCH = 0;
        private int offsetZCH = 0;

        private double impulseXCH = 392;
        private double impulseYCH = 12800;
        private double impulseZCH = 4000;

        private int locationZCH;

        #endregion

        #region 光谱仪变量

        private bool spectralIsJoin;

        private ClassSpectralMeter classSpectralMeter = new ClassSpectralMeter();
        private ClassSpectralMeter.Param param;
        private ClassSpectralMeter.Result result;

        #endregion

        #region 光强板变量

        private ClassLuxBoard classLuxBoard = new ClassLuxBoard();
        private bool luxIsJoin;
        private string luxPortName;
        private double distance;
        private double luxStanValue;
        private double luxDarkValue;

        #endregion

        #region 测试变量

        private double gammaSample;//γ角度采样间隔
        private double gammaRange;
        private double thetaDegree;
        private double thetaSample;//Θ角度采样间隔------------------------------------------------------------------
        private double referFactor;//标准板参考系数(乘以)
        private int measProject;
        private int cycleSpace;
        private int measSpace;

        frmR4 frm_R4;
        frmR2 frm_R2;
        frmR3 frm_R3;
        frmR1 frm_R1;

        private bool singRIsMeas;
        private bool contRIsMeas;

        private double[] smallxData;
        private double[] smallyData;
        private double[] colorTempData;

        private double[][] gammaList;
        private double[][][] thetaList;
        private double[] wl;
        private int k = 0;


        private ArrayList smallxList = new ArrayList();
        private ArrayList smallyList = new ArrayList();
        private ArrayList colorTempList = new ArrayList();

        private Thread threadSingRMeas;
        private Thread threadContRMeas;



        frmL4 frm_L4;
        frmL4 frm_L4_1;   //对应波段二BSDF
        frmL2 frm_L2;
        frmL2 frm_L2_1;   //对应波段二BSDF
        frmL3 frm_L3;
        frmL1 frm_L1;

        private bool singLIsMeas;
        private bool contLIsMeas;
        
        private double[] luxData;
        private double[] luxData_1;          //对应波段二BSDF
        private double[][] luxThetaList;
        private double[][] luxThetaList_1;   //对应波段二BSDF

        private double totalLux;
        private ArrayList luxList = new ArrayList();
        private ArrayList luxList_1 = new ArrayList();   //对应波段二BSDF

        private Thread threadSingLMeas;
        private Thread threadContLMeas;

        private frmConfirmTheta frm_confirmTheta;

        private frmLumenType frm_lumenType; //强度类型选择窗口

        private int lumenType;  //强度类型

        frmR7 frm_R7;



        private bool singDIsMeas;
        private bool contDIsMeas;
        private Thread threadSingDMeas;
        private Thread threadContDMeas;



        private bool singBSDFIsMeas;
        private bool contBSDFIsMeas;

        private double wavelength1;
        private double wavelength2;
        private double wavelength3;   //对应波段二BSDF
        private double wavelength4;   //对应波段二BSDF

        private double trans;   //BSDF测量时滤光片透过率

        private Thread threadSingBSDFMeas;
        private Thread threadContBSDFMeas;
        
        #endregion

        #region 其它变量

        private ClassAppConfigure classAppConfigure;

        private delegate void dlgShowButton();
        private delegate void dlgDisplay();

        private double[] secondParam = new double[4];

        private double luxParam;

        private int layoutIndex = 0;

        private string code;
        private bool isAdmin = false;
        private bool isFmsAdmin = false;

        private string[] information = new string[6];

        private int sdcmIndex;
        private int lo;
        private int hi;

        private bool isMeas = false;

        private bool isExit = false;

        //判断是否为点击右上角x退出---------------------------------------------------------------
        private bool isFormClosing = true;

        #endregion

        #region 控制卡函数

        private int joinCtrl()
        {
            int ret = -1;

            if (ClassCtrlBorad.d1000_board_init() > 0)
            {
                /** 设置控制卡脉冲输出模式
                 * param : 轴号
                 * param : 0 pulse/dir模式，脉冲上升沿有效
                 *         1 pulse/dir模式，脉冲下降沿有效
                 *         2 CW/CCW模式，脉冲上升沿有效
                 *         3 CW/CCW模式，脉冲下降沿有效
                 */
                ClassCtrlBorad.d1000_set_pls_outmode(YCH, 0);
                ClassCtrlBorad.d1000_set_pls_outmode(XCH, 1);

                ClassCtrlBorad.d1000_out_bit(1, 0);
                // 公转回到原点
                ClassCtrlBorad.d1000_home_move(YCH, -strVelYCH, -maxVelYCH, tacc);
                // 等待公转回到原点
                while (ClassCtrlBorad.d1000_check_done(YCH) != 4) { }
                // 公转正向偏移
                ClassCtrlBorad.d1000_start_t_move(YCH, 19200, strVelYCH, maxVelYCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(YCH) == 0) { }
                // 公转偏移设零
                ClassCtrlBorad.d1000_set_command_pos(YCH, 0);
                // 公转正确位置
                ClassCtrlBorad.d1000_start_ta_move(YCH, offsetYCH, strVelYCH, maxVelYCH, tacc);
                // 设置计数为零
                ClassCtrlBorad.d1000_set_command_pos(YCH, 0);
                // 自转回到限制位
                string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                string flag = status.Substring(status.Length - 2, 1);
                if (flag != "1")
                {
                    ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                    // 等待自转回到限制位
                    while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
                }
                // 自转偏移设零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 自转正确位置
                ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                // 设置计数为零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 自转移动到负90度
                ClassCtrlBorad.d1000_start_ta_move(XCH, (int)(-1 * 392 * 50), strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }

                ret = 0;
                
            }
            else
            {
                ret = 1;
            }

            return ret;
        }

        private void restoreCtrl()
        {
            /** 设置控制卡脉冲输出模式
                 * param : 轴号
                 * param : 0 pulse/dir模式，脉冲上升沿有效
                 *         1 pulse/dir模式，脉冲下降沿有效
                 *         2 CW/CCW模式，脉冲上升沿有效
                 *         3 CW/CCW模式，脉冲下降沿有效
                 */
            ClassCtrlBorad.d1000_set_pls_outmode(YCH, 0);
            ClassCtrlBorad.d1000_set_pls_outmode(XCH, 0);

            ClassCtrlBorad.d1000_out_bit(1, 0);
            // 公转回到原点
            ClassCtrlBorad.d1000_home_move(YCH, -strVelYCH, -maxVelYCH, tacc);
            // 自转回到限制位
            string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
            string flag = status.Substring(status.Length - 2, 1);
            if (flag != "1")
            {
                ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
            }
            // 等待公转回到原点
            while (ClassCtrlBorad.d1000_check_done(YCH) != 4) { }
            // 等待自转回到限制位
            while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
            // 公转正向偏移(1.5°？)--------------------------------------------------------------------
            ClassCtrlBorad.d1000_start_t_move(YCH, 19200, strVelYCH, maxVelYCH, tacc);
            while (ClassCtrlBorad.d1000_check_done(YCH) == 0) { }
            // 公转偏移设零
            ClassCtrlBorad.d1000_set_command_pos(YCH, 0);
            // 自转偏移设零
            ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
            // 公转正确位置
            ClassCtrlBorad.d1000_start_ta_move(YCH, offsetYCH, strVelYCH, maxVelYCH, tacc);
            // 自转正确位置
            ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
            while (ClassCtrlBorad.d1000_check_done(YCH) == 0) { }
            while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
            // 设置计数为零
            ClassCtrlBorad.d1000_set_command_pos(YCH, 0);
            ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
            // 自转移动到负90度
            ClassCtrlBorad.d1000_start_ta_move(XCH, -392 * 50, strVelXCH, maxVelXCH, tacc);
            while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
        }

        private void exitCtrl()
        {
            locationZCH = ClassCtrlBorad.d1000_get_command_pos(ZCH);

            ClassCtrlBorad.d1000_immediate_stop(YCH);
            ClassCtrlBorad.d1000_immediate_stop(XCH);
            ClassCtrlBorad.d1000_immediate_stop(ZCH);

            ClassCtrlBorad.d1000_out_bit(1, 0);

            ClassCtrlBorad.d1000_board_close();
        }

        #endregion

        #region 测试函数

        //测量标准板参考值------------------------------------------------------------------------------------------
        private void referValueMeasure()
        {
            double[] referValue = new double[5];
            double sum = 0;
            double meanValue;
            if (ctrlIsJoin)
            {
                if (MessageBox.Show("请放上标准板，按确定开始测试!", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    int verticalPos = (int)impulseXCH * (-140);
                    ClassCtrlBorad.d1000_start_ta_move(XCH, verticalPos, strVelXCH, maxVelXCH, tacc);   //γ转到竖直位置

                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }

                    for (int i = 0; i < 5; i++)
                    {
                        Thread.Sleep(500);
                        classSpectralMeter.Measure(param, 380d, 830d, ref result);
                        calibrateBSDF();
                        referValue[i] = result.uWatt;
                        
                        sum += result.uWatt;

                    }
                    meanValue = sum / 5;
                    referFactor = 0.93 / (meanValue * 3.14159);
                    MessageBox.Show(this, "标准板参考值为" + meanValue.ToString("0.000") + " 系数为" + referFactor.ToString("0.###E+0") + "，请换上测试样品后按确定继续！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            else
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        private void caliRMeasure()
        {
            result.x += secondParam[1];
            result.y += secondParam[2];
            result.colorTemp += secondParam[3];
        }

        private void analyzeRMeasure()
        {
            int l = (int)(2 * gammaRange / gammaSample + 1);

            smallxData = new double[l];
            smallyData = new double[l];
            colorTempData = new double[l];

            smallxList.CopyTo(0, smallxData, 0, l);
            smallyList.CopyTo(0, smallyData, 0, l);
            colorTempList.CopyTo(0, colorTempData, 0, l);

            if (Math.IEEERemainder(5, gammaSample) == 0)
            {
                int length = (int)gammaRange / 5;
                double[][] temp = new double[length + 1][];

                for (int i = 0; i < temp.Length; i++)
                {
                    temp[i] = new double[47];
                    gammaList[length - i].CopyTo(temp[i], 0);
                }

                thetaList[(int)thetaDegree / 15] = temp;

                temp = new double[19][];

                for (int i = 0; i < 19; i++)
                {
                    temp[i] = new double[47];
                    gammaList[length + i].CopyTo(temp[i], 0);
                }

                thetaList[12 + (int)thetaDegree / 15] = temp;
            }
        }

        private void analyzeRMeasure2()
        {
            wl = new double[83 - 38 + 1];
            double[] cs = new double[83 - 38 + 1];

            for (int i = 0; i <= 83 - 38; i++)
            {
                int jj = search(result.calcSpec, (38 + i) * 10);
                wl[i] = result.waveleng[search(result.waveleng, (38 + i) * 10)] / 1000;
                cs[i] = result.calcSpec[search(result.waveleng, (38 + i) * 10)];
            }

            gammaList[k] = cs;
        }

        private int search(double[] data, double value)
        {
            int index = 0;
            double distance = Math.Abs(data[0] - value);
            for (int i = 1; i < data.Length; i++)
            {
                if (Math.Abs(data[i] - value) < distance)
                {
                    index = i;
                    distance = Math.Abs(data[i] - value);
                }
            }
            return index;
        }

        private void displayRMeasure()
        {
            frm_R4.GammaSample = gammaSample;
            frm_R4.GammaRange = gammaRange;
            frm_R4.ThetaDegree = thetaDegree;
            frm_R4.SmallxData = smallxData;
            frm_R4.SmallyData = smallyData;
            frm_R4.ColorTempData = colorTempData;
            frm_R4.Display();

            frm_R2.GammaSample = gammaSample;
            frm_R2.GammaRange = gammaRange;
            frm_R2.ThetaDegree = thetaDegree;
            frm_R2.ColorTempData = colorTempData;
            frm_R2.Display();

            frm_R3.SmallxData = smallxData;
            frm_R3.SmallyData = smallyData;
            frm_R3.Display();

            frm_R1.GammaSample = gammaSample;
            frm_R1.GammaRange = gammaRange;
            frm_R1.ThetaDegree = thetaDegree;
            frm_R1.ColorTempData = colorTempData;
            frm_R1.Display();

            information[2] = DateTime.Now.ToString("yyyy-MM-dd hh:mm");
        }

        private void singRMeasure()
        {
            if (singRIsMeas)
            {
                int count = 0;

                BeginInvoke(new dlgLabelDisplay(labelDisplay), "正在测量！");

                ClassCtrlBorad.d1000_start_ta_move(YCH, (int)(thetaDegree * impulseYCH), strVelYCH, maxVelYCH, tacc);
                ClassCtrlBorad.d1000_start_ta_move(XCH, (int)((140 - gammaRange) * (int)impulseXCH) * -1, strVelXCH, maxVelXCH, tacc);

                while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                {
                    Thread.Sleep(1);
                }
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                {
                    Thread.Sleep(1);
                }

                smallxList.Clear();
                smallyList.Clear();
                colorTempList.Clear();

                classSourceMeter.Set(sourceMode, sptSource, voltSet, currSet);
                classSourceMeter.LightOn(sourceMode, sptSource);

                int dist = (int)(gammaSample * (int)impulseXCH);
                k = 0;

                do
                {
                    Thread.Sleep(measSpace);    //改为测试前等待一段时间
                    classSpectralMeter.Measure(param, 380d, 830d, ref result);
                    caliRMeasure();
                    smallxList.Add(result.x);
                    smallyList.Add(result.y);
                    colorTempList.Add(result.colorTemp);
                    count++;

                    int loc = ClassCtrlBorad.d1000_get_command_pos(XCH);

                    if (Math.IEEERemainder(loc, impulseXCH * 5) == 0)
                    {
                        analyzeRMeasure2();
                        k++;
                    }

                    ClassCtrlBorad.d1000_start_t_move(XCH, -1 * dist, strVelXCH, maxVelXCH, tacc);

                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }

                    //Thread.Sleep(measSpace);
                }
                while (count != (int)(2 * gammaRange / gammaSample));

                if (count == (int)(2 * gammaRange / gammaSample))
                {
                    classSpectralMeter.Measure(param, 380d, 830d, ref result);
                    caliRMeasure();
                    smallxList.Add(result.x);
                    smallyList.Add(result.y);
                    colorTempList.Add(result.colorTemp);
                    count++;

                    int loc = ClassCtrlBorad.d1000_get_command_pos(XCH);

                    if (Math.IEEERemainder(loc, impulseXCH * 5) == 0)
                    {
                        analyzeRMeasure2();
                        k++;
                    }

                    analyzeRMeasure();
                    classSourceMeter.Measure(sourceMode, sptSource, ref voltCheck, ref currCheck);
                    BeginInvoke(new dlgDisplay(displayRMeasure));
                }

                classSourceMeter.LightOff(sourceMode, sptSource);

                //取消自转回原-------------------------------------------------------2015.12.07
                // 公转回到零点
                //ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                //while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                //{
                //    Thread.Sleep(1);
                //}
                //取消自转回原-------------------------------------------------------2015.12.07


                // 公转回到限制位
                string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                string flag = status.Substring(status.Length - 2, 1);
                if (flag != "1")
                {
                    ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                }
                // 等待公转回到限制位
                while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
                // 公转偏移设零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 公转正确位置
                ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                // 设置计数为零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 公转移动到负90度
                ClassCtrlBorad.d1000_start_ta_move(XCH, (int)(-1 * 392 * 50), strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) 
                {
                    Thread.Sleep(1);
                }
               

                singRIsMeas = false;

                BeginInvoke(new dlgShowButton(enableButton));
            }

            if (thetaDegree == 165)
            {
                thetaDegree = 0;
            }
            else
            {
                thetaDegree += 15;
            }

            BeginInvoke(new dlgLabelDisplay(labelDisplay), "完成测量！");

            threadSingRMeas.Abort();
        }

        private void contRMeasure()
        {
            if (contRIsMeas)
            {
                BeginInvoke(new dlgLabelDisplay(labelDisplay), "正在测量！");

                //增加Θ角度间隔-------------------------------------------------------------------------------------------
                //for (thetaDegree = 0; thetaDegree < 180; thetaDegree += 15)
                for (thetaDegree = 0; thetaDegree < 180; thetaDegree += thetaSample)
                {
                    int count = 0;

                    ClassCtrlBorad.d1000_start_ta_move(YCH, (int)(thetaDegree * impulseYCH), strVelYCH, maxVelYCH, tacc);
                    ClassCtrlBorad.d1000_start_ta_move(XCH, (int)((140 - gammaRange) * (int)impulseXCH) * -1, strVelXCH, maxVelXCH, tacc);

                    while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                    {
                        Thread.Sleep(1);
                    }

                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }

                    smallxList.Clear();
                    smallyList.Clear();
                    colorTempList.Clear();

                    classSourceMeter.Set(sourceMode, sptSource, voltSet, currSet);
                    classSourceMeter.LightOn(sourceMode, sptSource);

                    int dist = (int)(gammaSample * (int)impulseXCH);
                    k = 0;

                    do
                    {
                        Thread.Sleep(measSpace);    //改为测试前等待一段时间
                        classSpectralMeter.Measure(param, 380d, 830d, ref result);
                        caliRMeasure();
                        smallxList.Add(result.x);
                        smallyList.Add(result.y);
                        colorTempList.Add(result.colorTemp);
                        count++;

                        int loc = ClassCtrlBorad.d1000_get_command_pos(XCH);

                        if (Math.IEEERemainder(loc, impulseXCH * 5) == 0)
                        {
                            analyzeRMeasure2();
                            k++;
                        }

                        ClassCtrlBorad.d1000_start_t_move(XCH, -1 * dist, strVelXCH, maxVelXCH, tacc);
                        while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                        {
                            Thread.Sleep(1);
                        }

                        //Thread.Sleep(measSpace);
                    }
                    while (count != (int)(2 * gammaRange / gammaSample));

                    if (count == (int)(2 * gammaRange / gammaSample))
                    {
                        classSpectralMeter.Measure(param, 380d, 830d, ref result);
                        caliRMeasure();
                        smallxList.Add(result.x);
                        smallyList.Add(result.y);
                        colorTempList.Add(result.colorTemp);
                        count++;

                        int loc = ClassCtrlBorad.d1000_get_command_pos(XCH);

                        if (Math.IEEERemainder(loc, impulseXCH * 5) == 0)
                        {
                            analyzeRMeasure2();
                            k++;
                        }

                        analyzeRMeasure();
                        classSourceMeter.Measure(sourceMode, sptSource, ref voltCheck, ref currCheck);
                        BeginInvoke(new dlgDisplay(displayRMeasure));
                    }

                    classSourceMeter.LightOff(sourceMode, sptSource);

                    //取消自转回原-------------------------------------------------------2015.12.07
                    //// 自转回到零点
                    //ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                    //while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                    //{
                    //    Thread.Sleep(1);
                    //}
                    //取消自转回原-------------------------------------------------------2015.12.07


                    // 公转回到限制位
                    string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                    string flag = status.Substring(status.Length - 2, 1);
                    if (flag != "1")
                    {
                        ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                    }
                    // 等待公转回到限制位
                    while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
                    // 公转偏移设零
                    ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                    // 公转正确位置
                    ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                    // 设置计数为零
                    ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                    // 公转移动到负90度
                    ClassCtrlBorad.d1000_start_ta_move(XCH, (int)(-1 * 392 * 50), strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }
                }

                // 自转回到零点
                ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                {
                    Thread.Sleep(1);
                }

                contRIsMeas = false;

                BeginInvoke(new dlgShowButton(enableButton));
            }

            BeginInvoke(new dlgLabelDisplay(labelDisplay), "完成测量！");

            threadContRMeas.Abort();
        }

        private void caliLMeasure()
        {
            luxStanValue *= luxParam;
        }

        private void analyzeLMeasure()
        {
            int l = (int)(2 * gammaRange / gammaSample + 1);

            luxData = new double[l];

            luxList.CopyTo(0, luxData, 0, l);

            //取消平均--------------------------------------------------------------------------------------
            //for (int i = 1; i < luxData.Length - 1; i++)
            //{
            //    luxData[i] = (luxData[i - 1] + luxData[i] + luxData[i + 1]) / 3;
            //}

            double[] temp = new double[(luxData.Length + 1) / 2];

            for (int i = 0; i < temp.Length; i++)
            {
                temp[i] = luxData[temp.Length - i - 1];
            }
            luxThetaList[(int)thetaDegree / 15] = new double[temp.Length];
            temp.CopyTo(luxThetaList[(int)thetaDegree / 15], 0);

            temp = new double[(luxData.Length + 1) / 2];

            for (int i = 0; i < temp.Length; i++)
            {
                temp[i] = luxData[temp.Length + i - 1];
            }
            luxThetaList[12 + (int)thetaDegree / 15] = new double[temp.Length];
            temp.CopyTo(luxThetaList[12 + (int)thetaDegree / 15], 0);
        }

        private void displayLMeasure()
        {
            frm_L4.GammaSample = gammaSample;
            frm_L4.GammaRange = gammaRange;
            frm_L4.ThetaDegree = thetaDegree;
            frm_L4.LuxData = luxData;
            frm_L4.Display();

            frm_L2.GammaSample = gammaSample;
            frm_L2.GammaRange = gammaRange;
            frm_L2.ThetaDegree = thetaDegree;
            frm_L2.LuxData = luxData;
            frm_L2.Display();

            frm_L3.GammaSample = gammaSample;
            frm_L3.GammaRange = gammaRange;
            frm_L3.ThetaDegree = thetaDegree;
            frm_L3.LuxData = luxData;
            frm_L3.Display();

            frm_L1.GammaSample = gammaSample;
            frm_L1.GammaRange = gammaRange;
            frm_L1.ThetaDegree = thetaDegree;
            frm_L1.LuxData = luxData;
            frm_L1.Display();

            information[2] = DateTime.Now.ToString("yyyy-MM-dd hh:mm");
        }

        private void singLMeasure()
        {
            if (singLIsMeas)
            {
                int count = 0;

                BeginInvoke(new dlgLabelDisplay(labelDisplay), "正在测量！");

                ClassCtrlBorad.d1000_start_ta_move(YCH, (int)(thetaDegree * impulseYCH), strVelYCH, maxVelYCH, tacc);
                ClassCtrlBorad.d1000_start_ta_move(XCH, (int)((140 - gammaRange) * (int)impulseXCH) * -1, strVelXCH, maxVelXCH, tacc);

                while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                {
                    Thread.Sleep(1);
                }
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                {
                    Thread.Sleep(1);
                }

                luxList.Clear();

                classSourceMeter.Set(sourceMode, sptSource, voltSet, currSet);
                classSourceMeter.LightOn(sourceMode, sptSource);

                int dist = (int)(gammaSample * (int)impulseXCH);

                do
                {
                    Thread.Sleep(measSpace);    //改为测试前等待一段时间

                    if (lumenType == 1)   //强度类型为发光强度-------------------------------2016.04.08
                    {
                        classSpectralMeter.MeasureCandela(param, ref luxStanValue);
                        caliLMeasure();
                        luxList.Add(luxStanValue);
                    }
                    else if (lumenType == 2)//强度类型为辐射强度-------------------------------2016.04.08
                    {
                        wavelength1=frm_lumenType.Wavelength1;
                        wavelength2=frm_lumenType.Wavelength2;
                        classSpectralMeter.Measure(param, wavelength1, wavelength2, ref result);
                        calibrateBSDF();
                        luxList.Add(result.uWatt);
                    }

                    count++;

                    ClassCtrlBorad.d1000_start_t_move(XCH, -1 * dist, strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }

                    //Thread.Sleep(measSpace);
                }
                while (count != (int)(2 * gammaRange / gammaSample));

                if (count == (int)(2 * gammaRange / gammaSample))
                {
                    ////classLuxBoard.Measure(luxDarkValue, distance, ref luxStanValue);
                    //classSpectralMeter.MeasureCandela(param, ref luxStanValue);
                    ////luxStanValue = (luxStanValue - luxDarkValue) * Math.Pow(distance, 2d);
                    //caliLMeasure();
                    //luxList.Add(luxStanValue);

                    if (lumenType == 1)   //强度类型为发光强度-------------------------------2016.04.08
                    {
                        classSpectralMeter.MeasureCandela(param, ref luxStanValue);
                        caliLMeasure();
                        luxList.Add(luxStanValue);
                    }
                    else if (lumenType == 2)//强度类型为辐射强度-------------------------------2016.04.08
                    {
                        classSpectralMeter.Measure(param, wavelength1, wavelength2, ref result);
                        calibrateBSDF();
                        luxList.Add(result.uWatt);
                    }

                    count++;



                    analyzeLMeasure();
                    classSourceMeter.Measure(sourceMode, sptSource, ref voltCheck, ref currCheck);
                    BeginInvoke(new dlgDisplay(displayLMeasure));
                }

                classSourceMeter.LightOff(sourceMode, sptSource);

                //取消自转回原-------------------------------------------------------2015.12.07
                //ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                //while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                //{
                //    Thread.Sleep(1);
                //}
                //取消自转回原-------------------------------------------------------2015.12.07


                // 公转回到限制位
                string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                string flag = status.Substring(status.Length - 2, 1);
                if (flag != "1")
                {
                    ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                }
                // 等待公转回到限制位
                while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
                // 公转偏移设零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 公转正确位置
                ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                // 设置计数为零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 公转移动到负90度
                ClassCtrlBorad.d1000_start_ta_move(XCH, (int)(-1 * 392 * 50), strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                {
                    Thread.Sleep(1);
                }

                singLIsMeas = false;

                BeginInvoke(new dlgShowButton(enableButton));
            }

            if (thetaDegree == 165)
            {
                thetaDegree = 0;
            }
            else
            {
                thetaDegree += 15;
            }

            BeginInvoke(new dlgLabelDisplay(labelDisplay), "完成测量！");

            threadSingLMeas.Abort();
        }

        private void contLMeasure()
        {
            if (contLIsMeas)
            {
                BeginInvoke(new dlgLabelDisplay(labelDisplay), "正在测量！");

                //增加Θ角度间隔-------------------------------------------------------------------------------------------
                //for (thetaDegree = 0; thetaDegree < 180; thetaDegree += 15)
                for (thetaDegree = 0; thetaDegree < 180; thetaDegree += thetaSample)
                {
                    int count = 0;

                    ClassCtrlBorad.d1000_start_ta_move(YCH, (int)(thetaDegree * impulseYCH), strVelYCH, maxVelYCH, tacc);
                    ClassCtrlBorad.d1000_start_ta_move(XCH, (int)((140 - gammaRange) * (int)impulseXCH) * -1, strVelXCH, maxVelXCH, tacc);
                    
                    while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                    {
                        Thread.Sleep(1);
                    }
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }

                    luxList.Clear();

                    classSourceMeter.Set(sourceMode, sptSource, voltSet, currSet);
                    classSourceMeter.LightOn(sourceMode, sptSource);

                    int dist = (int)(gammaSample * (int)impulseXCH);

                    do
                    {
                        Thread.Sleep(measSpace);    //改为测试前等待一段时间
                        classSpectralMeter.MeasureCandela(param, ref luxStanValue);
                        caliLMeasure();
                        luxList.Add(luxStanValue);
                        count++;

                        ClassCtrlBorad.d1000_start_t_move(XCH, -1 * dist, strVelXCH, maxVelXCH, tacc);
                        while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                        {
                            Thread.Sleep(1);
                        }

                        //Thread.Sleep(measSpace);
                    }
                    while (count != (int)(2 * gammaRange / gammaSample));

                    if (count == (int)(2 * gammaRange / gammaSample))
                    {
                        classSpectralMeter.MeasureCandela(param, ref luxStanValue);
                        caliLMeasure();
                        luxList.Add(luxStanValue);
                        count++;

                        analyzeLMeasure();
                        classSourceMeter.Measure(sourceMode, sptSource, ref voltCheck, ref currCheck);
                        BeginInvoke(new dlgDisplay(displayLMeasure));
                    }

                    classSourceMeter.LightOff(sourceMode, sptSource);

                    //取消自转回原-------------------------------------------------------2015.12.07
                    //// 自转回到零点
                    //ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                    //while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                    //{
                    //    Thread.Sleep(1);
                    //}
                    //取消自转回原-------------------------------------------------------2015.12.07


                    // 公转回到限制位
                    string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                    string flag = status.Substring(status.Length - 2, 1);
                    if (flag != "1")
                    {
                        ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                    }
                    // 等待公转回到限制位
                    while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
                    // 公转偏移设零
                    ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                    // 公转正确位置
                    ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                    // 设置计数为零
                    ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                    // 公转移动到负90度
                    ClassCtrlBorad.d1000_start_ta_move(XCH, (int)(-1 * 392 * 50), strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }
                }

                // 自转回到零点
                ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                {
                    Thread.Sleep(1);
                }

                contLIsMeas = false;

                BeginInvoke(new dlgShowButton(enableButton));
            }

            BeginInvoke(new dlgLabelDisplay(labelDisplay), "完成测量！");

            threadContLMeas.Abort();
        }


        private void calibrateBSDF()
        {
            result.uWatt *= luxParam;
        }

        private void analyzeBSDFMeasure()
        {
            int l = (int)(2 * gammaRange / gammaSample + 1);

            luxData = new double[l];

            luxList.CopyTo(0, luxData, 0, l);

            for (int i = 0; i < luxData.Length; i++)
            {
                //luxData[i] = (luxData[i - 1] + luxData[i] + luxData[i + 1]) / 3;
                luxData[i] *= referFactor;  //乘以标准板参考系数
                luxData[i] /= trans;    //除以滤光片透过率--------------------------2016-08-25
            }

            
            double[] temp = new double[(luxData.Length + 1) / 2];

            for (int i = 0; i < temp.Length; i++)
            {
                temp[i] = luxData[temp.Length - i - 1];
            }
            luxThetaList[(int)thetaDegree / 15] = new double[temp.Length];

            temp.CopyTo(luxThetaList[(int)thetaDegree / 15], 0);

            temp = new double[(luxData.Length + 1) / 2];

            for (int i = 0; i < temp.Length; i++)
            {
                temp[i] = luxData[temp.Length + i - 1];
            }
            luxThetaList[12 + (int)thetaDegree / 15] = new double[temp.Length];
            temp.CopyTo(luxThetaList[12 + (int)thetaDegree / 15], 0);
            
            Array.Clear(temp, 0, temp.Length);//---------------------------------------------------清除数组数据
            //temp.Initialize();//---------------------------------------------------清除数组数据

        }

        //对应波段二BSDF-------------------------------------------------------------2016.02.27
        private void analyzeBSDFMeasure_1()
        {
            int l = (int)(2 * gammaRange / gammaSample + 1);

            luxData_1 = new double[l];

            luxList_1.CopyTo(0, luxData_1, 0, l);

            for (int i = 0; i < luxData_1.Length; i++)
            {
                //luxData[i] = (luxData[i - 1] + luxData[i] + luxData[i + 1]) / 3;
                luxData_1[i] *= referFactor;  //乘以标准板参考系数
            }

            double[] temp_1 = new double[(luxData_1.Length + 1) / 2];

            for (int i = 0; i < temp_1.Length; i++)
            {
                temp_1[i] = luxData_1[temp_1.Length - i - 1];
            }
            luxThetaList_1[(int)thetaDegree / 15] = new double[temp_1.Length];
            temp_1.CopyTo(luxThetaList_1[(int)thetaDegree / 15], 0);

            temp_1 = new double[(luxData_1.Length + 1) / 2];

            for (int i = 0; i < temp_1.Length; i++)
            {
                temp_1[i] = luxData_1[temp_1.Length + i - 1];
            }
            luxThetaList_1[12 + (int)thetaDegree / 15] = new double[temp_1.Length];
            temp_1.CopyTo(luxThetaList_1[12 + (int)thetaDegree / 15], 0);

            Array.Clear(temp_1, 0, temp_1.Length);//---------------------------------------------------清除数组数据
            //temp_1.Initialize();//---------------------------------------------------清除数组数据

        }

        //显示双波段BSDF测量结果-----------------------------------------------------2016.02.27
        private void displayBiBSDFMeasure()
        {
            //修改双波段BSDF测量时的窗体标题---------------------------------------------------------------2016.02.27
            frm_L4.Text = "波段" + "(" + wavelength1.ToString() + "nm-" + wavelength2.ToString() + "nm)";
            frm_L2.Text = "波段" + "(" + wavelength1.ToString() + "nm-" + wavelength2.ToString() + "nm)";

            frm_L4_1.Text = "波段" + "(" + wavelength3.ToString() + "nm-" + wavelength4.ToString() + "nm)";
            frm_L2_1.Text = "波段" + "(" + wavelength3.ToString() + "nm-" + wavelength4.ToString() + "nm)";
            //--------------------------------------------------------------------------------------------2016.02.27

            frm_L4.GammaSample = gammaSample;
            frm_L4.GammaRange = gammaRange;
            frm_L4.ThetaDegree = thetaDegree;
            frm_L4.LuxData = luxData;
            frm_L4.Display();

            frm_L4_1.GammaSample = gammaSample;
            frm_L4_1.GammaRange = gammaRange;
            frm_L4_1.ThetaDegree = thetaDegree;
            frm_L4_1.LuxData = luxData_1;
            frm_L4_1.Display();

            frm_L2.GammaSample = gammaSample;
            frm_L2.GammaRange = gammaRange;
            frm_L2.ThetaDegree = thetaDegree;
            frm_L2.LuxData = luxData;
            frm_L2.Display();

            frm_L2_1.GammaSample = gammaSample;
            frm_L2_1.GammaRange = gammaRange;
            frm_L2_1.ThetaDegree = thetaDegree;
            frm_L2_1.LuxData = luxData_1;
            frm_L2_1.Display();

            information[2] = DateTime.Now.ToString("yyyy-MM-dd hh:mm");
        }

        private void singBSDFMeasure()
        {
            if (singBSDFIsMeas)
            {
                int count = 0;

                BeginInvoke(new dlgLabelDisplay(labelDisplay), "正在测量！");

                ClassCtrlBorad.d1000_start_ta_move(YCH, (int)(thetaDegree * impulseYCH), strVelYCH, maxVelYCH, tacc);
                ClassCtrlBorad.d1000_start_ta_move(XCH, (int)((140 - gammaRange) * (int)impulseXCH) * -1, strVelXCH, maxVelXCH, tacc);

                while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                {
                    Thread.Sleep(1);
                }
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                {
                    Thread.Sleep(1);
                }

                luxList.Clear();

                if (measProject == 10)     //对应波段二BSDF
                {
                    luxList_1.Clear();
                }

                classSourceMeter.Set(sourceMode, sptSource, voltSet, currSet);
                classSourceMeter.LightOn(sourceMode, sptSource);

                int dist = (int)(gammaSample * (int)impulseXCH);

                do
                {
                    Thread.Sleep(measSpace);    //改为测试前等待一段时间
                    classSpectralMeter.Measure(param, wavelength1, wavelength2, ref result);
                    calibrateBSDF();
                    luxList.Add(result.uWatt);

                    //双波段BSDF测量波段二的能量值----------------------------------------------------2016.02.27
                    if (measProject == 10)
                    {
                        classSpectralMeter.Measure(param, wavelength3, wavelength4, ref result);
                        calibrateBSDF();
                        luxList_1.Add(result.uWatt);
                    }
                    //------------------------------------------------------------------------------2016.02.27

                    count++;

                    ClassCtrlBorad.d1000_start_t_move(XCH, -1 * dist, strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }

                    //Thread.Sleep(measSpace);
                }
                while (count != (int)(2 * gammaRange / gammaSample));

                if (count == (int)(2 * gammaRange / gammaSample))
                {
                    classSpectralMeter.Measure(param, wavelength1, wavelength2, ref result);
                    calibrateBSDF();
                    luxList.Add(result.uWatt);

                    //双波段BSDF测量波段二的能量值----------------------------------------------------2016.02.27
                    if (measProject == 10)
                    {
                        classSpectralMeter.Measure(param, wavelength3, wavelength4, ref result);
                        calibrateBSDF();
                        luxList_1.Add(result.uWatt);
                    }
                    //------------------------------------------------------------------------------2016.02.27

                    count++;

                    analyzeBSDFMeasure();

                    //分析波段二的测量结果-----------------------------------------------------------2016.02.27
                    if (measProject == 10)
                    {
                        analyzeBSDFMeasure_1();
                    }
                    //------------------------------------------------------------------------------2016.02.27

                    classSourceMeter.Measure(sourceMode, sptSource, ref voltCheck, ref currCheck);

                    if (measProject == 10)      //显示双波段BSDF测量结果
                    {
                        BeginInvoke(new dlgDisplay(displayBiBSDFMeasure));
                    }
                    else
                    {
                        BeginInvoke(new dlgDisplay(displayLMeasure));
                    }
                }

                classSourceMeter.LightOff(sourceMode, sptSource);

                //取消自转回原-------------------------------------------------------2015.12.07
                //ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                //while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                //{
                //    Thread.Sleep(1);
                //}
                //取消自转回原-------------------------------------------------------2015.12.07


                // 公转回到限制位
                string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                string flag = status.Substring(status.Length - 2, 1);
                if (flag != "1")
                {
                    ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                }
                // 等待公转回到限制位
                while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
                // 公转偏移设零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 公转正确位置
                ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                // 设置计数为零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 公转移动到负90度
                ClassCtrlBorad.d1000_start_ta_move(XCH, (int)(-1 * 392 * 50), strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                {
                    Thread.Sleep(1);
                }

                singBSDFIsMeas = false;

                BeginInvoke(new dlgShowButton(enableButton));
            }

            if (thetaDegree == 165)
            {
                thetaDegree = 0;
            }
            else
            {
                thetaDegree += 15;
            }

            BeginInvoke(new dlgLabelDisplay(labelDisplay), "完成测量！");

            //threadSingLMeas.Abort();
            threadSingBSDFMeas.Abort();
        }

        private void contBSDFMeasure()
        {
            if (contBSDFIsMeas)
            {
                BeginInvoke(new dlgLabelDisplay(labelDisplay), "正在测量！");

                //增加Θ角度间隔-------------------------------------------------------------------------------------------
                //for (thetaDegree = 0; thetaDegree < 180; thetaDegree += 15)
                for (thetaDegree = 0; thetaDegree < 180; thetaDegree += thetaSample)
                {
                    int count = 0;

                    ClassCtrlBorad.d1000_start_ta_move(YCH, (int)(thetaDegree * impulseYCH), strVelYCH, maxVelYCH, tacc);
                    ClassCtrlBorad.d1000_start_ta_move(XCH, (int)((140 - gammaRange) * (int)impulseXCH) * -1, strVelXCH, maxVelXCH, tacc);

                    while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                    {
                        Thread.Sleep(1);
                    }
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }

                    luxList.Clear();

                    if (measProject == 10)     //对应波段二BSDF
                    {
                        luxList_1.Clear();
                    }

                    classSourceMeter.Set(sourceMode, sptSource, voltSet, currSet);
                    classSourceMeter.LightOn(sourceMode, sptSource);

                    int dist = (int)(gammaSample * (int)impulseXCH);

                    do
                    {
                        Thread.Sleep(measSpace);    //改为测试前等待一段时间
                        classSpectralMeter.Measure(param, wavelength1, wavelength2, ref result);
                        calibrateBSDF();
                        luxList.Add(result.uWatt);

                        //双波段BSDF测量波段二的能量值----------------------------------------------------2016.02.27
                        if (measProject == 10)
                        {
                            classSpectralMeter.Measure(param, wavelength3, wavelength4, ref result);
                            calibrateBSDF();
                            luxList_1.Add(result.uWatt);
                        }
                        //------------------------------------------------------------------------------2016.02.27

                        count++;

                        ClassCtrlBorad.d1000_start_t_move(XCH, -1 * dist, strVelXCH, maxVelXCH, tacc);
                        while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                        {
                            Thread.Sleep(1);
                        }

                        //Thread.Sleep(measSpace);
                    }
                    while (count != (int)(2 * gammaRange / gammaSample));

                    if (count == (int)(2 * gammaRange / gammaSample))
                    {
                        classSpectralMeter.Measure(param, wavelength1, wavelength2, ref result);
                        calibrateBSDF();
                        luxList.Add(result.uWatt);

                        //双波段BSDF测量波段二的能量值----------------------------------------------------2016.02.27
                        if (measProject == 10)
                        {
                            classSpectralMeter.Measure(param, wavelength3, wavelength4, ref result);
                            calibrateBSDF();
                            luxList_1.Add(result.uWatt);
                        }
                        //-------------------------------------------------------------------------------2016.02.27

                        count++;

                        analyzeBSDFMeasure();

                        //分析波段二的测量结果------------------------------------------------------------2016.02.27
                        if (measProject == 10)
                        {
                            analyzeBSDFMeasure_1();
                        }
                        //-------------------------------------------------------------------------------2016.02.27

                        classSourceMeter.Measure(sourceMode, sptSource, ref voltCheck, ref currCheck);
                        
                        if (measProject == 10)      //显示双波段BSDF测量结果
                        {
                            BeginInvoke(new dlgDisplay(displayBiBSDFMeasure));
                        }
                        else
                        {
                            BeginInvoke(new dlgDisplay(displayLMeasure));
                        }
                    }

                    classSourceMeter.LightOff(sourceMode, sptSource);

                    //取消自转回原-------------------------------------------------------2015.12.07
                    //// 自转回到零点
                    //ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                    //while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                    //{
                    //    Thread.Sleep(1);
                    //}
                    //取消自转回原-------------------------------------------------------2015.12.07


                    // 公转回到限制位
                    string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                    string flag = status.Substring(status.Length - 2, 1);
                    if (flag != "1")
                    {
                        ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                    }
                    // 等待公转回到限制位
                    while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
                    // 公转偏移设零
                    ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                    // 公转正确位置
                    ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                    // 设置计数为零
                    ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                    // 公转移动到负90度
                    ClassCtrlBorad.d1000_start_ta_move(XCH, (int)(-1 * 392 * 50), strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }
                }

                // 自转回到零点
                ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                {
                    Thread.Sleep(1);
                }

                contBSDFIsMeas = false;

                BeginInvoke(new dlgShowButton(enableButton));
            }

            BeginInvoke(new dlgLabelDisplay(labelDisplay), "完成测量！");
            MessageBox.Show("测量完成!", "系统提示", MessageBoxButtons.OK , MessageBoxIcon.Information);
            threadContBSDFMeas.Abort();
        }


        private void displayDMeasure()
        {
            frm_L4.GammaSample = gammaSample;
            frm_L4.GammaRange = gammaRange;
            frm_L4.ThetaDegree = thetaDegree;
            frm_L4.LuxData = luxData;
            frm_L4.Display();

            frm_R4.GammaSample = gammaSample;
            frm_R4.GammaRange = gammaRange;
            frm_R4.ThetaDegree = thetaDegree;
            frm_R4.SmallxData = smallxData;
            frm_R4.SmallyData = smallyData;
            frm_R4.ColorTempData = colorTempData;
            frm_R4.Display();

            frm_L2.GammaSample = gammaSample;
            frm_L2.GammaRange = gammaRange;
            frm_L2.ThetaDegree = thetaDegree;
            frm_L2.LuxData = luxData;
            frm_L2.Display();

            frm_R2.GammaSample = gammaSample;
            frm_R2.GammaRange = gammaRange;
            frm_R2.ThetaDegree = thetaDegree;
            frm_R2.ColorTempData = colorTempData;
            frm_R2.Display();

            information[2] = DateTime.Now.ToString("yyyy-MM-dd hh:mm");
        }

        private void singDMeasure()
        {
            if (singDIsMeas)
            {
                int count = 0;

                BeginInvoke(new dlgLabelDisplay(labelDisplay), "正在测量！");

                ClassCtrlBorad.d1000_start_ta_move(YCH, (int)(thetaDegree * impulseYCH), strVelYCH, maxVelYCH, tacc);
                ClassCtrlBorad.d1000_start_ta_move(XCH, (int)((140 - gammaRange) * (int)impulseXCH) * -1, strVelXCH, maxVelXCH, tacc);

                while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                {
                    Thread.Sleep(1);
                }
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                {
                    Thread.Sleep(1);
                }

                smallxList.Clear();
                smallyList.Clear();
                colorTempList.Clear();
                luxList.Clear();

                classSourceMeter.Set(sourceMode, sptSource, voltSet, currSet);
                classSourceMeter.LightOn(sourceMode, sptSource);

                int dist = (int)(gammaSample * (int)impulseXCH);
                k = 0;

                do
                {
                    Thread.Sleep(measSpace);    //改为测试前等待一段时间
                    classSpectralMeter.Measure(param, 380d, 830d, ref result);
                    caliRMeasure();
                    luxStanValue = result.candela;
                    caliLMeasure();
                    smallxList.Add(result.x);
                    smallyList.Add(result.y);
                    colorTempList.Add(result.colorTemp);
                    luxList.Add(luxStanValue);
                    count++;

                    int loc = ClassCtrlBorad.d1000_get_command_pos(XCH);

                    if (Math.IEEERemainder(loc, impulseXCH * 5) == 0)
                    {
                        analyzeRMeasure2();
                        k++;
                    }

                    ClassCtrlBorad.d1000_start_t_move(XCH, -1 * dist, strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }

                    //Thread.Sleep(measSpace);
                }
                while (count != (int)(2 * gammaRange / gammaSample));

                if (count == (int)(2 * gammaRange / gammaSample))
                {
                    classSpectralMeter.Measure(param, 380d, 830d, ref result);
                    caliRMeasure();
                    luxStanValue = result.candela;
                    caliLMeasure();
                    smallxList.Add(result.x);
                    smallyList.Add(result.y);
                    colorTempList.Add(result.colorTemp);
                    luxList.Add(luxStanValue);
                    count++;

                    int loc = ClassCtrlBorad.d1000_get_command_pos(XCH);

                    if (Math.IEEERemainder(loc, impulseXCH * 5) == 0)
                    {
                        analyzeRMeasure2();
                        k++;
                    }

                    analyzeRMeasure();
                    analyzeLMeasure();
                    classSourceMeter.Measure(sourceMode, sptSource, ref voltCheck, ref currCheck);
                    BeginInvoke(new dlgDisplay(displayDMeasure));
                }

                classSourceMeter.LightOff(sourceMode, sptSource);

                //取消自转回原点---------------------------------------------------------2015.12.07
                //// 自转回到零点
                //ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                //while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                //{
                //    Thread.Sleep(1);
                //}
                //取消自转回原点---------------------------------------------------------2015.12.07



                // 自转回到限制位
                string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                string flag = status.Substring(status.Length - 2, 1);
                if (flag != "1")
                {
                    ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                }
                // 等待公转回到限制位
                while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
                // 公转偏移设零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 公转正确位置
                ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                // 设置计数为零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 公转移动到负90度
                ClassCtrlBorad.d1000_start_ta_move(XCH, (int)(-1 * 392 * 50), strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                {
                    Thread.Sleep(1);
                }

                singDIsMeas = false;
            }

            if (thetaDegree == 165)
            {
                thetaDegree = 0;
            }
            else
            {
                thetaDegree += 15;
            }

            BeginInvoke(new dlgShowButton(enableButton));

            threadSingDMeas.Abort();
        }

        private void contDMeasure()
        {
            if (contDIsMeas)
            {
                BeginInvoke(new dlgLabelDisplay(labelDisplay), "正在测量！");


                //增加Θ角度间隔-------------------------------------------------------------------------------------------
                //for (thetaDegree = 0; thetaDegree < 180; thetaDegree += 15)
                for (thetaDegree = 0; thetaDegree < 180; thetaDegree += thetaSample)
                {
                    int count = 0;

                    ClassCtrlBorad.d1000_start_ta_move(YCH, (int)(thetaDegree * impulseYCH), strVelYCH, maxVelYCH, tacc);
                    ClassCtrlBorad.d1000_start_ta_move(XCH, (int)((140 - gammaRange) * (int)impulseXCH) * -1, strVelXCH, maxVelXCH, tacc);

                    while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                    {
                        Thread.Sleep(1);
                    }
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }

                    smallxList.Clear();
                    smallyList.Clear();
                    colorTempList.Clear();
                    luxList.Clear();

                    classSourceMeter.Set(sourceMode, sptSource, voltSet, currSet);
                    classSourceMeter.LightOn(sourceMode, sptSource);

                    int dist = (int)(gammaSample * (int)impulseXCH);
                    k = 0;

                    do
                    {
                        Thread.Sleep(measSpace);    //改为测试前等待一段时间
                        classSpectralMeter.Measure(param,380d,830d, ref result);
                        caliRMeasure();
                        luxStanValue = result.candela;
                        caliLMeasure();
                        smallxList.Add(result.x);
                        smallyList.Add(result.y);
                        colorTempList.Add(result.colorTemp);
                        luxList.Add(luxStanValue);
                        count++;

                        int loc = ClassCtrlBorad.d1000_get_command_pos(XCH);

                        if (Math.IEEERemainder(loc, impulseXCH * 5) == 0)
                        {
                            analyzeRMeasure2();
                            k++;
                        }

                        ClassCtrlBorad.d1000_start_t_move(XCH, -1 * dist, strVelXCH, maxVelXCH, tacc);
                        while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                        {
                            Thread.Sleep(1);
                        }

                        //Thread.Sleep(measSpace);
                    }
                    while (count != (int)(2 * gammaRange / gammaSample));

                    if (count == (int)(2 * gammaRange / gammaSample))
                    {
                        classSpectralMeter.Measure(param, 380d, 830d, ref result);
                        caliRMeasure();
                        luxStanValue = result.candela;
                        caliLMeasure();
                        smallxList.Add(result.x);
                        smallyList.Add(result.y);
                        colorTempList.Add(result.colorTemp);
                        luxList.Add(luxStanValue);
                        count++;

                        int loc = ClassCtrlBorad.d1000_get_command_pos(XCH);

                        if (Math.IEEERemainder(loc, impulseXCH * 5) == 0)
                        {
                            analyzeRMeasure2();
                            k++;
                        }

                        analyzeRMeasure();
                        analyzeLMeasure();
                        classSourceMeter.Measure(sourceMode, sptSource, ref voltCheck, ref currCheck);
                        BeginInvoke(new dlgDisplay(displayDMeasure));
                    }

                    classSourceMeter.LightOff(sourceMode, sptSource);

                    ////取消自转回原点---------------------------------------------------------2015.12.07
                    //// 公转回到零点
                    //ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                    //while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                    //{
                    //    Thread.Sleep(1);
                    //}
                    //取消自转回原点---------------------------------------------------------2015.12.07


                    // 公转回到限制位
                    string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                    string flag = status.Substring(status.Length - 2, 1);
                    if (flag != "1")
                    {
                        ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                    }
                    // 等待公转回到限制位
                    while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
                    // 公转偏移设零
                    ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                    // 公转正确位置
                    ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                    // 设置计数为零
                    ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                    // 公转移动到负90度
                    ClassCtrlBorad.d1000_start_ta_move(XCH, (int)(-1 * 392 * 50), strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0)
                    {
                        Thread.Sleep(1);
                    }
                }

                // 自转回到零点
                ClassCtrlBorad.d1000_start_ta_move(YCH, 0, strVelYCH, maxVelYCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(YCH) == 0)
                {
                    Thread.Sleep(1);
                }

                contDIsMeas = false;

                BeginInvoke(new dlgShowButton(enableButton));
            }

            BeginInvoke(new dlgLabelDisplay(labelDisplay), "完成测量！");

            MessageBox.Show("测量完成!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            threadContDMeas.Abort();

        }

        #endregion

        #region 其它函数

        private void loadConfig()
        {
            classAppConfigure = new ClassAppConfigure(Application.StartupPath + "\\data\\ini.xml");
            tllLux.Visible = false;

            #region 电源

            sourceMode = classAppConfigure.SourceMode;
            sourcePortName = classAppConfigure.SourcePortName;
            voltSet = classAppConfigure.VoltSet;
            currSet = classAppConfigure.CurrSet;
            preheatTime = classAppConfigure.PreheatTime;

            #endregion

            #region 控制卡

            strVelXCH = classAppConfigure.StrVelXCH;
            strVelYCH = classAppConfigure.StrVelYCH;
            strVelZCH = classAppConfigure.StrVelZCH;
            maxVelXCH = classAppConfigure.MaxVelXCH;
            maxVelYCH = classAppConfigure.MaxVelYCH;
            maxVelZCH = classAppConfigure.MaxVelZCH;
            tacc = classAppConfigure.Tacc;
            offsetXCH = classAppConfigure.OffsetXCH;
            offsetYCH = classAppConfigure.OffsetYCH;
            offsetZCH = classAppConfigure.OffsetZCH;
            locationZCH = classAppConfigure.LocationZCH;

            #endregion

            #region 光谱仪

            param.IsAutoAdjust = classAppConfigure.IsAutoAdjust;
            param.IntTime = classAppConfigure.IntTime;
            param.STA = classAppConfigure.STA;
            param.BCW = classAppConfigure.BCW;

            #endregion

            #region 光强板

            luxPortName = classAppConfigure.LuxBoardPortName;
            distance = classAppConfigure.Distance;
            luxDarkValue = classAppConfigure.LuxDarkValue;

            #endregion

            #region 测试

            gammaSample = classAppConfigure.GammaSample;
            gammaRange = classAppConfigure.GammaRange;
            cycleSpace = classAppConfigure.CycleSpace;
            measSpace = classAppConfigure.MeasSpace;
            secondParam[0] = classAppConfigure.SecondParam0;
            secondParam[1] = classAppConfigure.SecondParam1;
            secondParam[2] = classAppConfigure.SecondParam2;
            secondParam[3] = classAppConfigure.SecondParam3;
            luxParam = classAppConfigure.LuxParam;

            #endregion

            #region 其它

            code = classAppConfigure.Code;

            information[0] = classAppConfigure.Information0;
            information[1] = classAppConfigure.Information1;
            information[2] = classAppConfigure.Information2;
            information[3] = classAppConfigure.Information3;
            information[4] = classAppConfigure.Information4;
            information[5] = classAppConfigure.Information5;

            sdcmIndex = classAppConfigure.SDCMIndex;
            lo = classAppConfigure.RankLo;
            hi = classAppConfigure.RankHi;

            #endregion
        }

        private void saveConfig()
        {
            #region 电源

            classAppConfigure.SourceMode = sourceMode;
            classAppConfigure.SourcePortName = sourcePortName;
            classAppConfigure.VoltSet = voltSet;
            classAppConfigure.CurrSet = currSet;
            classAppConfigure.PreheatTime = preheatTime;

            #endregion

            #region 控制卡

            classAppConfigure.StrVelXCH = strVelXCH;
            classAppConfigure.StrVelYCH = strVelYCH;
            classAppConfigure.StrVelZCH = strVelZCH;
            classAppConfigure.MaxVelXCH = maxVelXCH;
            classAppConfigure.MaxVelYCH = maxVelYCH;
            classAppConfigure.MaxVelZCH = maxVelZCH;
            classAppConfigure.Tacc = tacc;
            classAppConfigure.OffsetXCH = offsetXCH;
            classAppConfigure.OffsetYCH = offsetYCH;
            classAppConfigure.OffsetZCH = offsetZCH;
            classAppConfigure.LocationZCH = locationZCH;

            #endregion

            #region 光谱仪

            classAppConfigure.IsAutoAdjust = param.IsAutoAdjust;
            classAppConfigure.IntTime = param.IntTime;
            classAppConfigure.STA = param.STA;
            classAppConfigure.BCW = param.BCW;

            #endregion

            #region 光强板

            classAppConfigure.LuxBoardPortName = luxPortName;
            classAppConfigure.Distance = distance;
            classAppConfigure.LuxDarkValue = luxDarkValue;

            #endregion

            #region 测试

            classAppConfigure.GammaSample = gammaSample;
            classAppConfigure.GammaRange = gammaRange;
            classAppConfigure.CycleSpace = cycleSpace;
            classAppConfigure.MeasSpace = measSpace;
            classAppConfigure.SecondParam0 = secondParam[0];
            classAppConfigure.SecondParam1 = secondParam[1];
            classAppConfigure.SecondParam2 = secondParam[2];
            classAppConfigure.SecondParam3 = secondParam[3];
            classAppConfigure.LuxParam = luxParam;

            #endregion

            #region 其它

            classAppConfigure.Code = code;

            classAppConfigure.Information0 = information[0];
            classAppConfigure.Information1 = information[1];
            classAppConfigure.Information2 = information[2];
            classAppConfigure.Information3 = information[3];
            classAppConfigure.Information4 = information[4];
            classAppConfigure.Information5 = information[5];

            classAppConfigure.SDCMIndex = sdcmIndex;
            classAppConfigure.RankLo = lo;
            classAppConfigure.RankHi = hi;

            #endregion

            classAppConfigure.Save();
        }

        private void initButton()
        {
            tlmSingle.Enabled = false;
            tlmContin.Enabled = false;
            tlmStop.Enabled = false;

            toolStripMenuItem11.Visible = false;

            tlmSDCM.Visible = false;
            tlmWindow.Visible = false;

            tlSingle.Enabled = false;
            tlContin.Enabled = false;
            tlStop.Enabled = false;
        }

        private void initRButton()
        {
            tlmSingle.Enabled = true;
            tlmContin.Enabled = true;
            tlmStop.Enabled = true;

            tlmSDCM.Visible = true;
            tlmWindow.Visible = true;
            toolStripMenuItem11.Visible = true;
            tlmPolar.Visible = true;
            tlm2D.Visible = true;
            tlmCIE.Visible = true;
            tlm3D.Visible = false;
            tlmList.Visible = true;

            tlSingle.Enabled = true;
            tlContin.Enabled = true;
            tlStop.Enabled = true;
        }

        private void initDButton()
        {
            tlmSingle.Enabled = true;
            tlmContin.Enabled = true;
            tlmStop.Enabled = true;

            tlmSDCM.Visible = true;
            tlmWindow.Visible = false;
            toolStripMenuItem11.Visible = false;

            tlSingle.Enabled = true;
            tlContin.Enabled = true;
            tlStop.Enabled = true;
        }

        private void initLButton()
        {
            tlmSingle.Enabled = true;
            tlmContin.Enabled = true;
            tlmStop.Enabled = true;

            toolStripMenuItem11.Visible = true;

            tlmSDCM.Visible = true;
            toolStripMenuItem9.Visible = true;
            tlmWindow.Visible = true;
            tlmPolar.Visible = true;
            tlm2D.Visible = true;
            tlmCIE.Visible = false;
            tlm3D.Visible = true;
            tlmList.Visible = true;

            tlSingle.Enabled = true;
            tlContin.Enabled = true;
            tlStop.Enabled = true;
        }

        private void unableButton()
        {
            tlmNew.Enabled = false;

            tlmSingle.Enabled = false;
            tlmContin.Enabled = false;
            tlmDark.Enabled = false;
            tlmRestore.Enabled = false;

            tlmRSecCalibration.Enabled = false;

            tlmManual.Enabled = false;
            tlmLocation.Enabled = false;

            tlNew.Enabled = false;
            tlClear.Enabled = false;
            tlSingle.Enabled = false;
            tlContin.Enabled = false;
        }

        private void enableButton()
        {
            tlmNew.Enabled = true;

            tlmSingle.Enabled = true;
            tlmContin.Enabled = true;
            tlmDark.Enabled = true;
            tlmRestore.Enabled = true;

            tlmRSecCalibration.Enabled = true;
            tlmLSecCalibration.Enabled = true;

            tlmManual.Enabled = true;
            tlmLocation.Enabled = true;

            tlNew.Enabled = true;
            tlClear.Enabled = true;
            tlSingle.Enabled = true;
            tlContin.Enabled = true;
        }

        private void exportIES(string fileName)
        {
            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default))
            {
                int count = 0;
                for (int i = 0; i < luxThetaList.Length; i++)
                {
                    if (luxThetaList[i] != null)
                    {
                        count++;
                    }
                }
                StringBuilder sb = new StringBuilder();
                sb.Append("IESNA:LM-63-2002").Append("\r\n");
                sb.Append("[TEST]" + DateTime.Now.ToString("yyMMdd")).Append("\r\n");
                sb.Append("[TESTLAB]" + "LED Tester").Append("\r\n");
                sb.Append("[ISSUEDATE]" + DateTime.Now.ToString("M/d/yyyy h:m:s")).Append("\r\n");
                sb.Append("[MANUFAC]FMS").Append("\r\n");
                sb.Append("TILT=NONE").Append("\r\n");
                sb.Append("1 1 1 " + (gammaRange / gammaSample + 1).ToString() + " " + count.ToString() + " 1 1 0.0 0.0 0.0").Append("\r\n");
                sb.Append("1.00 1.00 0").Append("\r\n");
                
                for (int i = 0; i < (gammaRange / gammaSample + 1); i++)
                {
                    sb.Append((i * gammaSample).ToString("0.0") + " ");
                }

                sb.Append("\r\n");

                for (int i = 0; i < luxThetaList.Length; i++)
                {
                    if (luxThetaList[i] != null)
                    {
                        sb.Append((i * 15).ToString("0.0") + " ");
                    }
                }
                for (int i = 0; i < luxThetaList.Length; i++)
                {
                    sb.Append("\r\n");

                    if (luxThetaList[i] != null)
                    {
                        for (int j = 0; j < luxThetaList[i].Length; j++)
                        {
                            sb.Append(luxThetaList[i][j] + " ");
                        }
                    }
                }

                sw.WriteLine(sb.ToString());

                sw.WriteLine(sb.ToString());
                sw.Flush();
                sw.Close();
            }
        }

        private void exportCandela(string fileName) //修改换行--------------------------------------------------------------
        {
            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default))
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("\r\n");
                sb.Append("TracePro Release: 7 0 4 \r\n");
                sb.Append("Polar Candela Distribution Data for  " + fileName + "\r\n");
                sb.Append("Data covers +/- 180.000 degrees from Normal\r\n");
                sb.Append("Data for Missed Rays Data generated at " + DateTime.Now.ToString("hh:mm:ss M dd, yyyy") + "\r\n");
                sb.Append("\r\n");


                //修改为i++-----------------------------------------------------------------------------------------------
                //for (int i = 0; i < luxThetaList.Length; i += 2)
                for (int i = 0; i < luxThetaList.Length; i++)
                {
                    if (luxThetaList[i] != null)
                    {
                        sb.Append("\t ").Append((i * 15).ToString("#0.0"));
                    }
                }
                sb.Append("\r\n");

                double[][] temp = new double[24][];

                luxThetaList.CopyTo(temp, 0);

                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i] != null)
                    {
                        for (int j = 0; j < temp[i].Length; j++)
                        {
                            temp[i][j] /= totalLux;
                        }
                    }
                }

                for (int i = 0; i < (gammaRange / gammaSample + 1); i += 1)
                {
                    sb.Append(" ").Append((i * gammaSample).ToString("#0.000")).Append("\t");

                    for (int j = 0; j < temp.Length; j += 1)
                    {
                        if (temp[j] != null)
                        {
                            sb.Append(temp[j][i]).Append("\t ");
                        }
                    }
                    sb.Append("\r\n");
                }

                sw.WriteLine(sb.ToString());
                sw.Flush();
                sw.Close();
            }
        }

        //输出波段二BSDF文件----------------------------------------------------------------------------------------2016.02.27
        private void exportCandela_1(string fileName) //修改换行----------------------------
        {
            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default))
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("\r\n");
                sb.Append("TracePro Release: 7 0 4 \r\n");
                sb.Append("Polar Candela Distribution Data for  " + fileName + "\r\n");
                sb.Append("Data covers +/- 180.000 degrees from Normal\r\n");
                sb.Append("Data for Missed Rays Data generated at " + DateTime.Now.ToString("hh:mm:ss M dd, yyyy") + "\r\n");
                sb.Append("\r\n");


                //修改为i++-----------------------------------------------------------------
                //for (int i = 0; i < luxThetaList.Length; i += 2)
                for (int i = 0; i < luxThetaList_1.Length; i++)
                {
                    if (luxThetaList_1[i] != null)
                    {
                        sb.Append("\t ").Append((i * 15).ToString("#0.0"));
                    }
                }
                sb.Append("\r\n");

                double[][] temp = new double[24][];

                luxThetaList_1.CopyTo(temp, 0);

                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i] != null)
                    {
                        for (int j = 0; j < temp[i].Length; j++)
                        {
                            temp[i][j] /= totalLux;
                        }
                    }
                }

                for (int i = 0; i < (gammaRange / gammaSample + 1); i += 1)
                {
                    sb.Append(" ").Append((i * gammaSample).ToString("#0.000")).Append("\t");

                    for (int j = 0; j < temp.Length; j += 1)
                    {
                        if (temp[j] != null)
                        {
                            sb.Append(temp[j][i]).Append("\t ");
                        }
                    }
                    sb.Append("\r\n");
                }

                sw.WriteLine(sb.ToString());
                sw.Flush();
                sw.Close();
            }
        }

        private delegate void dlgLabelDisplay(string str);

        private void labelDisplay(string str)
        {
            tllMeasStatus.Text = str;
        }

        #endregion

        #region 窗体事件

        private delegate void dlgStart();

        private void frmMain_Load(object sender, EventArgs e)
        {
            loadConfig();

            initButton();

            this.Show();

            #region 连接设备

            if (classSourceMeter.Join(sourceMode, sptSource, sourcePortName, 19200) == 0)
            {
                sourceIsJoin = true;
                tllSource.Text = "电源已连接";
                tllSource.ForeColor = Color.Black;
            }
            else
            {
                sourceIsJoin = false;
                tllSource.Text = "电源未连接";
                tllSource.ForeColor = Color.Red;
            }

            if (ctrlIsJoin)
            {
                return;
            }

            int ret = joinCtrl();

            if (ret == 0)
            {
                ctrlIsJoin = true;
                tllCtrl.Text = "控制卡已连接";
                tllCtrl.ForeColor = Color.Black;
            }
            else if (ret == 101)
            {
                ctrlIsJoin = false;
                tllCtrl.Text = "电闸未开或通讯线未连接";
                tllCtrl.ForeColor = Color.Red;
            }
            else if (ret == 1)
            {
                ctrlIsJoin = false;
                tllCtrl.Text = "控制卡未连接";
                tllCtrl.ForeColor = Color.Red;
            }

            if (classSpectralMeter.Join() == 0)
            {
                spectralIsJoin = true;
                tllSpectral.Text = "光谱仪已连接";
                tllSpectral.ForeColor = Color.Black;
            }
            else
            {
                spectralIsJoin = false;
                tllSpectral.Text = "光谱仪未连接";
                tllSpectral.ForeColor = Color.Red;
            }
            //----------------------------------------------------------------------------------------------------
            //if (classLuxBoard.Join(luxPortName) == 0)
            //{
            //    luxIsJoin = true;
            //    tllLux.Text = "光强板已连接";
            //    tllLux.ForeColor = Color.Black;
            //}
            //else
            //{
            //    luxIsJoin = false;
            //    tllLux.Text = "光强板未连接";
            //    tllLux.ForeColor = Color.Red;
            //}

            #endregion

            BeginInvoke(new dlgLabelDisplay(labelDisplay), "准备测量！");
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (singRIsMeas || contRIsMeas || singLIsMeas || contLIsMeas || singDIsMeas || contDIsMeas || singBSDFIsMeas || contBSDFIsMeas)//-----------------------
            {
                MessageBox.Show("正在测试！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            
            //----------------------------------------------------------------------------------------------------------------------
            if (isFormClosing)
            {
                if (MessageBox.Show("退出程序？", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    isFormClosing = false;

                    if (MessageBox.Show(this, "系统复位？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        // 公转回到原点
                        ClassCtrlBorad.d1000_home_move(YCH, -strVelYCH, -maxVelYCH, tacc);
                        // 等待公转回到原点
                        while (ClassCtrlBorad.d1000_check_done(YCH) != 4) { }
                        // 公转正向偏移
                        ClassCtrlBorad.d1000_start_t_move(YCH, 19200, strVelYCH, maxVelYCH, tacc);
                        while (ClassCtrlBorad.d1000_check_done(YCH) == 0) { }
                        // 公转偏移设零
                        ClassCtrlBorad.d1000_set_command_pos(YCH, 0);
                        // 公转正确位置
                        ClassCtrlBorad.d1000_start_ta_move(YCH, offsetYCH, strVelYCH, maxVelYCH, tacc);
                        // 设置计数为零
                        ClassCtrlBorad.d1000_set_command_pos(YCH, 0);

                        //γ转到竖直位置-------------------------------------------------------------------------
                        int verticalPos = (int)impulseXCH * (-140);
                        ClassCtrlBorad.d1000_start_ta_move(XCH, verticalPos, strVelXCH, maxVelXCH, tacc);
                        while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                    }

                    MessageBox.Show("请关闭设备电闸开关！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                    isExit = true;

                    #region 断开线程

                    if (sourceIsJoin)
                    {
                        classSourceMeter.LightOff(sourceMode, sptSource);
                    }
                    if (threadSingRMeas != null)
                    {
                        if (threadSingRMeas.IsAlive)
                        {
                            threadSingRMeas.Abort();
                        }
                    }
                    if (threadSingLMeas != null)
                    {
                        if (threadSingLMeas.IsAlive)
                        {
                            threadSingLMeas.Abort();
                        }
                    }
                    if (threadContRMeas != null)
                    {
                        if (threadContRMeas.IsAlive)
                        {
                            threadContRMeas.Abort();
                        }
                    }
                    if (threadContLMeas != null)
                    {
                        if (threadContLMeas.IsAlive)
                        {
                            threadContLMeas.Abort();
                        }
                    }

                    #endregion

                    #region 断开设备

                    if (sourceIsJoin)
                    {
                        classSourceMeter.Eixt(sourceMode, sptSource);
                    }
                    if (ctrlIsJoin)
                    {
                        exitCtrl();
                    }
                    if (spectralIsJoin)
                    {
                        classSpectralMeter.Exit();
                    }
                    if (luxIsJoin)
                    {
                        classLuxBoard.Exit();
                    }

                    #endregion

                    e.Cancel = false;

                    saveConfig();
                }

           
            }

                    

        }

        private void frmMain_SizeChanged(object sender, EventArgs e)
        {
            if (isExit)
            {
                return;
            }

            if (measProject == 0 && frm_R1 != null)
            {
                frm_R4.Select();
                frm_R2.Select();
                frm_R3.Select();
                frm_R1.Select();

                frm_R4.Show();
                frm_R2.Show();
                frm_R3.Show();
                frm_R1.Show();
            }
            else if (measProject == 1 && frm_L1 != null)
            {
                frm_L4.Select();
                frm_L2.Select();
                frm_L3.Select();
                frm_L1.Select();

                frm_L4.Show();
                frm_L2.Show();
                frm_L3.Show();
                frm_L1.Show();
            }

            if (layoutIndex == 0)
            {
                this.LayoutMdi(MdiLayout.TileVertical);
            }
            else if (layoutIndex == 1)
            {
                this.LayoutMdi(MdiLayout.Cascade);
            }
            else if (layoutIndex == 2)
            {
                this.LayoutMdi(MdiLayout.TileHorizontal);
            }
        }

        #endregion

        #region 按钮事件

        private void tlmNew_Click(object sender, EventArgs e)
        {
            frmNew frm_new = new frmNew();

            frm_new.MeasProject = measProject;

            frm_new.ShowDialog();

            if (frm_new.DialogResult == DialogResult.OK)
            {
                measProject = frm_new.MeasProject;

                initButton();

                #region 系统设置

                frmSetting frm_Setting = new frmSetting();

                isMeas = false;

                frm_Setting.GammaSample = gammaSample;
                frm_Setting.GammaRange = gammaRange;
                frm_Setting.CycleSpace = cycleSpace;
                frm_Setting.MeasSpace = measSpace;

                frm_Setting.IsAutoAdjust = param.IsAutoAdjust;
                frm_Setting.IntTime = param.IntTime;
                frm_Setting.STA = param.STA;
                frm_Setting.BCW = param.BCW;

                frm_Setting.LuxPortName = luxPortName;
                frm_Setting.Distance = distance;

                frm_Setting.SourceMode = sourceMode;
                frm_Setting.SourcePortName = sourcePortName;
                frm_Setting.Voltage = voltSet;
                frm_Setting.Current = currSet;
                frm_Setting.PreheatTime = preheatTime;

                frm_Setting.IsMeas = isMeas;

                frm_Setting.ShowDialog();

                if (frm_Setting.DialogResult == DialogResult.OK)
                {
                    //添加判断弹出BSDF波长窗口---------------------------------------------------------
                    if (measProject == 3)
                    {
                        frmBSDFwavelength frm_BSDFwavelength = new frmBSDFwavelength();
                        frm_BSDFwavelength.ShowDialog();
                        wavelength1 = frm_BSDFwavelength.Wavelength1;
                        wavelength2 = frm_BSDFwavelength.Wavelength2;

                        trans = frm_BSDFwavelength.Trans;   //增加滤光片透射率-------2016-08-25

                        referValueMeasure();
                    }

                    //添加判断弹出双波段BSDF波长窗口---------------------------------------------------------
                    if (measProject == 10)
                    {
                        frmBSDFwavelength_1 frm_BSDFwavelength_1 = new frmBSDFwavelength_1();
                        frm_BSDFwavelength_1.ShowDialog();
                        wavelength1 = frm_BSDFwavelength_1.Wavelength1;
                        wavelength2 = frm_BSDFwavelength_1.Wavelength2;
                        wavelength3 = frm_BSDFwavelength_1.Wavelength3;
                        wavelength4 = frm_BSDFwavelength_1.Wavelength4;
                        referValueMeasure();
                    }


                    gammaSample = frm_Setting.GammaSample;
                    gammaRange = frm_Setting.GammaRange;
                    cycleSpace = frm_Setting.CycleSpace;
                    measSpace = frm_Setting.MeasSpace;

                    param.IsAutoAdjust = frm_Setting.IsAutoAdjust;
                    param.IntTime = frm_Setting.IntTime;
                    param.STA = frm_Setting.STA;
                    param.BCW = frm_Setting.BCW;

                    luxPortName = frm_Setting.LuxPortName;
                    distance = frm_Setting.Distance;

                    sourceMode = frm_Setting.SourceMode;
                    sourcePortName = frm_Setting.SourcePortName;
                    voltSet = frm_Setting.Voltage;
                    currSet = frm_Setting.Current;
                    preheatTime = frm_Setting.PreheatTime;
                }
                else
                {
                    //initButton();
                    return;
                }

                #endregion

                #region 关闭窗体

                foreach (Form childForm in this.MdiChildren)
                {
                    childForm.Close();
                }

                #endregion

                #region LED色温空间分布测量

                if (measProject == 0)
                {
                    frm_confirmTheta = new frmConfirmTheta();

                    thetaDegree = 0;

                    isMeas = true;

                    thetaList = new double[24][][];
                    gammaList = new double[2 * (int)gammaRange / 5 + 1][];

                    frm_R4 = new frmR4();
                    frm_R2 = new frmR2();
                    frm_R3 = new frmR3();
                    frm_R1 = new frmR1();

                    frm_R4.MdiParent = this;
                    frm_R2.MdiParent = this;
                    frm_R3.MdiParent = this;
                    frm_R1.MdiParent = this;

                    frm_R4.GammaSample = gammaSample;
                    frm_R4.GammaRange = gammaRange;
                    frm_R4.ThetaDegree = thetaDegree;

                    frm_R2.GammaSample = gammaSample;
                    frm_R2.GammaRange = gammaRange;
                    frm_R2.ThetaDegree = thetaDegree;

                    frm_R1.GammaSample = gammaSample;
                    frm_R1.GammaRange = gammaRange;
                    frm_R1.ThetaDegree = thetaDegree;

                    frm_R4.Show();
                    frm_R2.Show();
                    frm_R3.Show();
                    frm_R1.Show();

                    LayoutMdi(MdiLayout.TileVertical);

                    initRButton();
                }

                #endregion

                #region LED光强空间分布测量

                else if (measProject == 1)
                {
                    frm_lumenType = new frmLumenType();//测光强是先弹出类型选择窗口

                    frm_lumenType.ShowDialog(); //测光强是先弹出类型选择窗口

                    if (frm_lumenType.DialogResult == DialogResult.OK)
                    {
                        lumenType = frm_lumenType.LumenType;
                        frm_lumenType.Close();
                    }



                   frm_confirmTheta = new frmConfirmTheta();

                   thetaDegree = 0;

                   isMeas = true;

                   luxThetaList = new double[24][];

                   frm_L4 = new frmL4();
                   frm_L2 = new frmL2();
                   frm_L3 = new frmL3();
                   frm_L1 = new frmL1();

                   frm_L4.MdiParent = this;
                   frm_L2.MdiParent = this;
                   frm_L3.MdiParent = this;
                   frm_L1.MdiParent = this;

                   frm_L4.GammaSample = gammaSample;
                   frm_L4.GammaRange = gammaRange;
                   frm_L4.ThetaDegree = thetaDegree;

                   frm_L2.GammaSample = gammaSample;
                   frm_L2.GammaRange = gammaRange;
                   frm_L2.ThetaDegree = thetaDegree;

                   frm_L3.GammaSample = gammaSample;
                   frm_L3.GammaRange = gammaRange;
                   frm_L3.ThetaDegree = thetaDegree;

                   frm_L1.GammaSample = gammaSample;
                   frm_L1.GammaRange = gammaRange;
                   frm_L1.ThetaDegree = thetaDegree;

                   frm_L4.Show();
                   frm_L2.Show();
                   frm_L3.Show();
                   frm_L1.Show();

                   LayoutMdi(MdiLayout.TileVertical);

                   initLButton();
                }

                #endregion

                #region LED光色空间分布同时测量

                else if (measProject == 2)
                {

                    frm_confirmTheta = new frmConfirmTheta();

                    thetaDegree = 0;

                    isMeas = true;

                    thetaList = new double[24][][];
                    gammaList = new double[2 * (int)gammaRange / 5 + 1][];

                    luxThetaList = new double[24][];

                    frm_L4 = new frmL4();
                    frm_R4 = new frmR4();
                    frm_L2 = new frmL2();
                    frm_R2 = new frmR2();

                    frm_L4.MdiParent = this;
                    frm_R4.MdiParent = this;
                    frm_L2.MdiParent = this;
                    frm_R2.MdiParent = this;


                    frm_R4.GammaSample = gammaSample;
                    frm_R4.GammaRange = gammaRange;
                    frm_R4.ThetaDegree = thetaDegree;

                    frm_R2.GammaSample = gammaSample;
                    frm_R2.GammaRange = gammaRange;
                    frm_R2.ThetaDegree = thetaDegree;

                    frm_L4.GammaSample = gammaSample;
                    frm_L4.GammaRange = gammaRange;
                    frm_L4.ThetaDegree = thetaDegree;

                    frm_L2.GammaSample = gammaSample;
                    frm_L2.GammaRange = gammaRange;
                    frm_L2.ThetaDegree = thetaDegree;

                    frm_L4.Show();
                    frm_R4.Show();
                    frm_L2.Show();
                    frm_R2.Show();


                    LayoutMdi(MdiLayout.TileVertical);

                    initDButton();
                }

                #endregion
                    

                #region BSDF双向散射分布函数测量
                else if (measProject == 3)
                {
                    frm_confirmTheta = new frmConfirmTheta();

                    thetaDegree = 0;

                    isMeas = true;

                    luxThetaList = new double[24][];

                    frm_L4 = new frmL4();
                    frm_L2 = new frmL2();
                    frm_L3 = new frmL3();
                    frm_L1 = new frmL1();

                    frm_L4.MdiParent = this;
                    frm_L2.MdiParent = this;
                    frm_L3.MdiParent = this;
                    frm_L1.MdiParent = this;

                    frm_L4.GammaSample = gammaSample;
                    frm_L4.GammaRange = gammaRange;
                    frm_L4.ThetaDegree = thetaDegree;

                    frm_L2.GammaSample = gammaSample;
                    frm_L2.GammaRange = gammaRange;
                    frm_L2.ThetaDegree = thetaDegree;

                    frm_L3.GammaSample = gammaSample;
                    frm_L3.GammaRange = gammaRange;
                    frm_L3.ThetaDegree = thetaDegree;

                    frm_L1.GammaSample = gammaSample;
                    frm_L1.GammaRange = gammaRange;
                    frm_L1.ThetaDegree = thetaDegree;

                    frm_L4.Show();
                    frm_L2.Show();
                    frm_L3.Show();
                    frm_L1.Show();

                    LayoutMdi(MdiLayout.TileVertical);
                    //referValueMeasure();
                    initLButton();

                }
                #endregion


                //添加双波段BSDF测量----------------------------------------------------------2016.02.27
                #region 双波段BSDF双向散射分布函数测量

                else if (measProject == 10)
                {
                    frm_confirmTheta = new frmConfirmTheta();

                    thetaDegree = 0;

                    isMeas = true;

                    luxThetaList = new double[24][];
                    luxThetaList_1 = new double[24][];

                    frm_L4 = new frmL4();
                    frm_L2 = new frmL2();
                    frm_L4_1 = new frmL4();
                    frm_L2_1 = new frmL2();

                    //修改双波段BSDF测量时的窗体标题---------------------------------------------------------------2016.02.27
                    frm_L4.Text = "波段" + "(" + wavelength1.ToString() + "nm-" + wavelength2.ToString() + "nm)";
                    frm_L2.Text = "波段" + "(" + wavelength1.ToString() + "nm-" + wavelength2.ToString() + "nm)";

                    frm_L4_1.Text = "波段" + "(" + wavelength3.ToString() + "nm-" + wavelength4.ToString() + "nm)";
                    frm_L2_1.Text = "波段" + "(" + wavelength3.ToString() + "nm-" + wavelength4.ToString() + "nm)";
                    //--------------------------------------------------------------------------------------------2016.02.27

                    frm_L4.MdiParent = this;
                    frm_L2.MdiParent = this;
                    frm_L4_1.MdiParent = this;
                    frm_L2_1.MdiParent = this;

                    frm_L4.GammaSample = gammaSample;
                    frm_L4.GammaRange = gammaRange;
                    frm_L4.ThetaDegree = thetaDegree;

                    frm_L2.GammaSample = gammaSample;
                    frm_L2.GammaRange = gammaRange;
                    frm_L2.ThetaDegree = thetaDegree;

                    frm_L4_1.GammaSample = gammaSample;
                    frm_L4_1.GammaRange = gammaRange;
                    frm_L4_1.ThetaDegree = thetaDegree;

                    frm_L2_1.GammaSample = gammaSample;
                    frm_L2_1.GammaRange = gammaRange;
                    frm_L2_1.ThetaDegree = thetaDegree;

                    frm_L4.Show();
                    frm_L2.Show();
                    frm_L4_1.Show();
                    frm_L2_1.Show();

                    LayoutMdi(MdiLayout.TileVertical);
                    //referValueMeasure();
                    initLButton();

                }
                #endregion


                #region LED色温电流变化测量

                else if (measProject == 4)
                {
                    frmR5 frm_R5 = new frmR5();

                    frm_R5.Class_SourceMeter = classSourceMeter;
                    frm_R5.SourceMode = sourceMode;
                    frm_R5.SPTSource = sptSource;
                    frm_R5.SourceIsJoin = sourceIsJoin;
                    frm_R5.VoltSet = voltSet;
                    frm_R5.CurrSet = currSet;

                    frm_R5.Class_SpectralMeter = classSpectralMeter;
                    frm_R5.SpectralIsJoin = spectralIsJoin;
                    frm_R5.Param = param;
                    frm_R5.Result = result;

                    frm_R5.SecondParam = secondParam;

                    frm_R5.Information = information;

                    frm_R5.MdiParent = this;

                    frm_R5.Show();
                }

                #endregion

                #region LED光强电流变化测量

                else if (measProject == 5)
                {
                    frmL5 frm_L5 = new frmL5();

                    frm_L5.Class_SourceMeter = classSourceMeter;
                    frm_L5.SourceMode = sourceMode;
                    frm_L5.SPTSource = sptSource;
                    frm_L5.SourceIsJoin = sourceIsJoin;
                    frm_L5.VoltSet = voltSet;
                    frm_L5.CurrSet = currSet;

                    frm_L5.Class_LuxBoard = classLuxBoard;
                    frm_L5.LuxIsJoin = luxIsJoin;
                    frm_L5.Distance = distance;
                    frm_L5.LuxDarkValue = luxDarkValue;

                    frm_L5.Class_SpectralMeter = classSpectralMeter;
                    frm_L5.SpectralIsJoin = spectralIsJoin;
                    frm_L5.Param = param;
                    frm_L5.Result = result;

                    frm_L5.LuxParam = luxParam;

                    frm_L5.Information = information;

                    frm_L5.MdiParent = this;

                    frm_L5.Show();
                }

                #endregion

                #region LED色温时间变化测量

                else if (measProject == 6)
                {
                    frmR6 frm_R6 = new frmR6();

                    frm_R6.Class_SourceMeter = classSourceMeter;
                    frm_R6.SourceMode = sourceMode;
                    frm_R6.SPTSource = sptSource;
                    frm_R6.SourceIsJoin = sourceIsJoin;
                    frm_R6.VoltSet = voltSet;
                    frm_R6.CurrSet = currSet;

                    frm_R6.Class_SpectralMeter = classSpectralMeter;
                    frm_R6.SpectralIsJoin = spectralIsJoin;
                    frm_R6.Param = param;
                    frm_R6.Result = result;

                    frm_R6.SecondParam = secondParam;

                    frm_R6.Information = information;

                    frm_R6.MdiParent = this;

                    frm_R6.Show();
                }

                #endregion

                #region LED光强时间变化测量

                else if (measProject == 7)
                {
                    frmL6 frm_L6 = new frmL6();

                    frm_L6.Class_SourceMeter = classSourceMeter;
                    frm_L6.SourceMode = sourceMode;
                    frm_L6.SPTSource = sptSource;
                    frm_L6.SourceIsJoin = sourceIsJoin;
                    frm_L6.VoltSet = voltSet;
                    frm_L6.CurrSet = currSet;

                    frm_L6.Class_LuxBoard = classLuxBoard;
                    frm_L6.LuxIsJoin = luxIsJoin;
                    frm_L6.Distance = distance;
                    frm_L6.LuxDarkValue = luxDarkValue;

                    frm_L6.Class_LuxBoard = classLuxBoard;
                    frm_L6.LuxIsJoin = luxIsJoin;
                    frm_L6.Distance = distance;
                    frm_L6.LuxDarkValue = luxDarkValue;

                    frm_L6.Information = information;

                    frm_L6.LuxParam = luxParam;

                    frm_L6.MdiParent = this;

                    frm_L6.Show();
                }

                #endregion

                #region LED光学参数测量

                else if (measProject == 8)
                {
                    tlmSDCM.Visible = true;

                    frm_R7 = new frmR7();

                    frm_R7.Class_SourceMeter = classSourceMeter;
                    frm_R7.SourceMode = sourceMode;
                    frm_R7.SPTSource = sptSource;
                    frm_R7.SourceIsJoin = sourceIsJoin;
                    frm_R7.VoltSet = voltSet;
                    frm_R7.CurrSet = currSet;

                    frm_R7.Class_SpectralMeter = classSpectralMeter;
                    frm_R7.SpectralIsJoin = spectralIsJoin;
                    frm_R7.Param = param;
                    frm_R7.Result = result;

                    frm_R7.SecondParam = secondParam;

                    frm_R7.Information = information;

                    frm_R7.MdiParent = this;

                    frm_R7.Show();
                }

                #endregion

                #region LED电学参数测量

                else if (measProject == 9)
                {
                    frmE1 frm_E1 = new frmE1();

                    frm_E1.Class_sourceMeter = classSourceMeter;
                    frm_E1.SourceMode = sourceMode;
                    frm_E1.SptSource = sptSource;
                    frm_E1.SourceIsJoin = sourceIsJoin;
                    frm_E1.VoltSet = voltSet;
                    frm_E1.CurrSet = currSet;

                    frm_E1.Information = information;

                    frm_E1.MdiParent = this;

                    frm_E1.Show();
                }

                #endregion


            }
        }

        private void tlmSaveXLS_Click(object sender, EventArgs e)
        {
            if (measProject == 0 && frm_R4 != null)
            {
                frm_R4.Information = information;
                frm_R4.Output();
            }
            else if (measProject == 1 && frm_L4 != null)
            {
                frm_L4.MeasFileName = " ";  //非双波段BSDF测量时文件名不包含波长信息；
                frm_L4.Information = information;
                frm_L4.Output();
            }

            //修改measProject---------------------------------------------------------------------------------------------
            else if (measProject == 2 && frm_R4 != null && frm_L4 != null)
            {
                frm_R4.Information = information;
                frm_R4.Output();
                frm_L4.Information = information;
                frm_L4.Output();
            }
            else if (measProject == 3 && frm_L4 != null)
            {
                frm_L4.MeasFileName = " ";  //非双波段BBSDF测量时文件名不包含波长信息；
                frm_L4.Information = information;
                frm_L4.Output();
            }

            else if (measProject == 10 && frm_L4 != null && frm_L4_1 != null)   //双波段BSDF测量列表保存
            {
                frm_L4.MeasFileName = "波段1" + "(" + wavelength1.ToString() + "nm-" + wavelength2.ToString() + "nm)";  //文件名波段一波长信息；
                frm_L4.Information = information;
                frm_L4.Output();

                frm_L4_1.MeasFileName = "波段2" + "(" + wavelength3.ToString() + "nm-" + wavelength4.ToString() + "nm)";  //文件名波段二波长信息；
                frm_L4_1.Information = information;
                frm_L4_1.Output();
            }
        }

        private void tlmSurfaceSpec_Click(object sender, EventArgs e)
        {
            if ((measProject != 0 && measProject != 2) || frm_R4 == null)
            {
                return;
            }
            if (colorTempData == null || colorTempData.Max() == 0)
            {
                return;
            }
            if (Math.IEEERemainder(5, gammaSample) != 0)
            {
                return;
            }

            SaveFileDialog sfdExportTXT = new SaveFileDialog();

            sfdExportTXT.FileName = "";
            sfdExportTXT.FilterIndex = 0;
            sfdExportTXT.Title = "另存为";
            sfdExportTXT.Filter = "文本文档(*.txt)|*.txt";

            if (sfdExportTXT.ShowDialog() == DialogResult.OK)
            {
                frmSurfaceSource frmsurfaceSource = new frmSurfaceSource();

                frmsurfaceSource.ShowDialog();

                if (frmsurfaceSource.DialogResult == DialogResult.OK)
                {
                    string sfName = frmsurfaceSource.SfName;
                    string sfCatalog = frmsurfaceSource.SfCatalog;

                    using (StreamWriter sw = new StreamWriter(sfdExportTXT.FileName, false, Encoding.Default))
                    {
                        double max = 0;

                        for (int i = 0; i < thetaList.Length; i++)
                        {
                            if (thetaList[i] != null)
                            {
                                for (int j = 0; j < thetaList[i].Length; j++)
                                {
                                    if (thetaList[i][j] != null)
                                    {
                                        if (thetaList[i][j].Max() > max)
                                        {
                                            max = thetaList[i][j].Max();
                                        }
                                    }
                                }
                            }
                        }

                        double[][][] temp = new double[24][][];

                        thetaList.CopyTo(temp, 0);

                        for (int i = 0; i < temp.Length; i++)
                        {
                            if (temp[i] != null)
                            {
                                for (int j = 0; j < temp[i].Length; j++)
                                {
                                    if (temp[i][j] != null)
                                    {
                                        for (int l = 0; l < temp[i][j].Length; l++)
                                        {
                                            temp[i][j][l] /= max;
                                        }
                                    }
                                }
                            }
                        }

                        StringBuilder sb = new StringBuilder();
                        sb.Append("TracePro Surface Source Property Data").Append("\r\n");
                        sb.Append("File Name\t" + sfdExportTXT.FileName).Append("\r\n");
                        sb.Append("TracePro Release: 7 0 3").Append("\r\n");
                        sb.Append("Database Version: 4 1 0").Append("\r\n");
                        sb.Append("Data generated at " + DateTime.Now.ToString("hh:mm:ss MM d, yyyy")).Append("\r\n").Append("\r\n");
                        sb.Append("Name\t" + sfName).Append("\r\n");
                        sb.Append("Catalog " + sfCatalog).Append("\r\n");
                        sb.Append("Description\tData from manufacturer's datasheet").Append("\r\n");
                        sb.Append("User_Data\t0").Append("\r\n");
                        sb.Append("Spectral Type\t3").Append("\r\n");
                        sb.Append("Angular Type\t4").Append("\r\n");
                        sb.Append("Units\t0").Append("\r\n");
                        sb.Append("Quantity\t1").Append("\r\n");
                        sb.Append("Emission\t1").Append("\r\n");
                        sb.Append("Wavelength1\t0").Append("\r\n");
                        sb.Append("Wavelength2\t0").Append("\r\n");
                        sb.Append("Angle1\t" + gammaRange).Append("\r\n");
                        sb.Append("Angle2\t15").Append("\r\n").Append("\r\n");
                        sb.Append("Temperature\tWavelength\tPolarAngle\tAzimuthAngle\tEmissivity").Append("\r\n");


                        for (int i = 0; i < temp[0][0].Length - 1; i++)
                        {
                            for (int j = 0; j < temp.Length; j++)
                            {
                                if (temp[j] != null)
                                {
                                    for (int l = 0; l < temp[j].Length; l++)
                                    {
                                        sb.Append("300\t" + wl[i] + "\t" + (l * 5).ToString() + "\t" + (j * 15).ToString() + "\t" + temp[j][l][i]).Append("\r\n");
                                    }
                                }
                            }
                        }

                        sw.WriteLine(sb.ToString());
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
        }

        private void tlmSaveIES_Click(object sender, EventArgs e)
        {
            if ((measProject != 1 && measProject != 2) || frm_L4 == null)
            {
                return;
            }
            if (luxData == null || luxData.Max() == 0)
            {
                return;
            }

            SaveFileDialog sfdExportIES = new SaveFileDialog();

            sfdExportIES.FileName = "";
            sfdExportIES.FilterIndex = 0;
            sfdExportIES.Title = "另存为";
            sfdExportIES.Filter = "配光曲线(*.ies)|*.ies";

            if (sfdExportIES.ShowDialog() == DialogResult.OK)
            {
                exportIES(sfdExportIES.FileName);
            }
        }

        private void tlmSaveCandela_Click(object sender, EventArgs e)
        {
            if ((measProject != 1 && measProject != 2 && measProject != 3 && measProject != 10) || frm_L4 == null)
            {
                return; //   || frm_L4 == null)
            }
            if (luxData == null || luxData.Max() == 0)
            {
                return;
            }

            SaveFileDialog sfdBSDF = new SaveFileDialog();

            sfdBSDF.FileName = "波段1" + "(" + wavelength1.ToString() + "nm-" + wavelength2.ToString() + "nm).txt";
            sfdBSDF.FilterIndex = 0;
            sfdBSDF.Title = "另存为";
            sfdBSDF.Filter = "文本文档(*.txt)|*.txt";

            if (sfdBSDF.ShowDialog() == DialogResult.OK)
            {
                if (measProject == 3 || measProject == 10)
                {
                    totalLux = 1;
                    exportCandela(sfdBSDF.FileName);
                }
                //else if (measProject == 10)  //输出双波段的BSDF文件
                //{
                //    totalLux = 1;
                //    exportCandela(sfdBSDF.FileName);
                //    exportCandela_1(sfdBSDF.FileName);
                //}
                else
                {
                    FrmBSDF frmBSDF = new FrmBSDF();

                    frmBSDF.ShowDialog();

                    if (frmBSDF.DialogResult == DialogResult.OK)
                    {
                        totalLux = frmBSDF.TotalLux;
                        exportCandela(sfdBSDF.FileName);
                    }
                }

            }

            if (measProject == 10)  //保存波段二BSDF文件
            {
                if (luxData_1 == null || luxData_1.Max() == 0)
                {
                    return;
                }

                SaveFileDialog sfdBSDF_1 = new SaveFileDialog();

                sfdBSDF_1.FileName = "波段2" + "(" + wavelength3.ToString() + "nm-" + wavelength4.ToString() + "nm).txt";
                sfdBSDF_1.FilterIndex = 0;
                sfdBSDF_1.Title = "另存为";
                sfdBSDF_1.Filter = "文本文档(*.txt)|*.txt";

                if (sfdBSDF_1.ShowDialog() == DialogResult.OK)
                {
                    totalLux = 1;
                    exportCandela_1(sfdBSDF_1.FileName);
                }
            }
        }

        private void tlmJoinSource_Click(object sender, EventArgs e)
        {
            if (classSourceMeter.Join(sourceMode, sptSource, sourcePortName, 19200) == 0)
            {
                sourceIsJoin = true;
                tllSource.Text = "电源已连接";
                tllSource.ForeColor = Color.Black;
            }
            else
            {
                sourceIsJoin = false;
                tllSource.Text = "电源未连接";
                tllSource.ForeColor = Color.Red;
            }
        }

        private void tlmJoinCtrl_Click(object sender, EventArgs e)
        {
            if (ctrlIsJoin)
            {
                return;
            }

            int ret = joinCtrl();

            if (ret == 0)
            {
                ctrlIsJoin = true;
                tllCtrl.Text = "控制卡已连接";
                tllCtrl.ForeColor = Color.Black;
            }
            else if (ret == 101)
            {
                ctrlIsJoin = false;
                tllCtrl.Text = "电闸未开或通讯线未连接";
                tllCtrl.ForeColor = Color.Red;
            }
            else if (ret == 1)
            {
                ctrlIsJoin = false;
                tllCtrl.Text = "控制卡未连接";
                tllCtrl.ForeColor = Color.Red;
            }
        }

        private void tlmJoinSpectral_Click(object sender, EventArgs e)
        {
            if (spectralIsJoin)
            {
                return;
            }

            if (classSpectralMeter.Join() == 0)
            {
                spectralIsJoin = true;
                tllSpectral.Text = "光谱仪已连接";
                tllSpectral.ForeColor = Color.Black;
            }
            else
            {
                spectralIsJoin = false;
                tllSpectral.Text = "光谱仪未连接";
                tllSpectral.ForeColor = Color.Red;
            }
        }

        private void tlmJoinLux_Click(object sender, EventArgs e)
        {
            if (luxIsJoin)
            {
                return;
            }

            if (classLuxBoard.Join(luxPortName) == 0)
            {
                luxIsJoin = true;
                tllLux.Text = "光强板已连接";
                tllLux.ForeColor = Color.Black;
            }
            else
            {
                luxIsJoin = false;
                tllLux.Text = "光强板未连接";
                tllLux.ForeColor = Color.Red;
            }
        }

        private void tlmPrintView_Click(object sender, EventArgs e)
        {
            ClassPrintPreviewDialogEx classPrintPreviewDialogEx = new ClassPrintPreviewDialogEx();
            classPrintPreviewDialogEx.Document = pdcTester;
            classPrintPreviewDialogEx.Show();
        }

        private void tlmPrint_Click(object sender, EventArgs e)
        {
            prdTester.Document = pdcTester;

            if (prdTester.ShowDialog() == DialogResult.OK)
            {
                pdcTester.Print();
            }
        }

        //增加复位选择----------------------------------------------------------------------------------------------------------------
        private void tlmExit_Click(object sender, EventArgs e)
        {
            if (singRIsMeas || contRIsMeas || singLIsMeas || contLIsMeas ||singBSDFIsMeas || contBSDFIsMeas)
            {
                MessageBox.Show("正在测试！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MessageBox.Show("退出程序？", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                isFormClosing = false;
                if(MessageBox.Show(this,"系统复位？","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)
                {
                    // 公转回到原点
                    ClassCtrlBorad.d1000_home_move(YCH, -strVelYCH, -maxVelYCH, tacc);
                    // 等待公转回到原点
                    while (ClassCtrlBorad.d1000_check_done(YCH) != 4) { }
                    // 公转正向偏移
                    ClassCtrlBorad.d1000_start_t_move(YCH, 19200, strVelYCH, maxVelYCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(YCH) == 0) { }
                    // 公转偏移设零
                    ClassCtrlBorad.d1000_set_command_pos(YCH, 0);
                    // 公转正确位置
                    ClassCtrlBorad.d1000_start_ta_move(YCH, offsetYCH, strVelYCH, maxVelYCH, tacc);
                    // 设置计数为零
                    ClassCtrlBorad.d1000_set_command_pos(YCH, 0);

                    int verticalPos = (int)impulseXCH * (-140);
                    ClassCtrlBorad.d1000_start_ta_move(XCH, verticalPos, strVelXCH, maxVelXCH, tacc);   //γ转到竖直位置
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                }

                MessageBox.Show("请关闭设备电闸开关！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                
                foreach (Form frm in this.MdiChildren)
                {
                    frm.Close();
                }

                Application.Exit();
            }
            else
            {
                MessageBox.Show("请关闭设备电闸开关！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void tlmSingle_Click(object sender, EventArgs e)
        {
            if (!sourceIsJoin)
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!ctrlIsJoin)
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!spectralIsJoin)
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            frm_confirmTheta.ThetaDegree = thetaDegree;

            frm_confirmTheta.ShowDialog();

            if (frm_confirmTheta.DialogResult == DialogResult.OK)
            {
                if (gammaRange >= 130 && thetaDegree != 0)
                {
                    return;
                }

                thetaDegree = frm_confirmTheta.ThetaDegree;

                if (measProject == 0)
                {
                    threadSingRMeas = new Thread(singRMeasure);
                    singRIsMeas = true;
                    unableButton();
                    threadSingRMeas.Start();
                }
                else if (measProject == 1)
                {
                    threadSingLMeas = new Thread(singLMeasure);
                    singLIsMeas = true;
                    unableButton();
                    threadSingLMeas.Start();
                }

                else if (measProject == 2)
                {
                    threadSingDMeas = new Thread(singDMeasure);
                    singDIsMeas = true;
                    unableButton();
                    threadSingDMeas.Start();
                }

                //添加BSDF线程-----------------------------------------------------------------------
                else if (measProject == 3)
                {
                    threadSingBSDFMeas = new Thread(singBSDFMeasure);
                    singBSDFIsMeas = true;
                    unableButton();
                    threadSingBSDFMeas.Start();
                }

                //添加双波段BSDF线程-----------------------------------------------------------------------
                else if (measProject == 10)
                {
                    threadSingBSDFMeas = new Thread(singBSDFMeasure);
                    singBSDFIsMeas = true;
                    unableButton();
                    threadSingBSDFMeas.Start();
                }

            }
        }

        private void tlmContin_Click(object sender, EventArgs e)
        {
            if (!sourceIsJoin)
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!ctrlIsJoin)
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (!spectralIsJoin)
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //增加C角度间隔选择窗口------------------------------------------------------------------------------------------------------
            frmThetaSample frm_ThetaSample = new frmThetaSample();
            frm_ThetaSample.ShowDialog();
            if (frm_ThetaSample.DialogResult == DialogResult.OK)
            {
                thetaSample = frm_ThetaSample.ThetaSample;
            }
            //--------------------------------------------------------------------------------------------------------------------------

            if (measProject == 0)
            {
                threadContRMeas = new Thread(contRMeasure);
                contRIsMeas = true;
                unableButton();
                threadContRMeas.Start();
            }
            else if (measProject == 1)
            {
                threadContLMeas = new Thread(contLMeasure);
                contLIsMeas = true;
                unableButton();
                threadContLMeas.Start();
            }
            else if (measProject == 2)
            {
                threadContDMeas = new Thread(contDMeasure);
                contDIsMeas = true;
                unableButton();
                threadContDMeas.Start();
            }

            //BSDF连续测量------------------------------------------------------------------------------------------------------------------------------------
            else if (measProject == 3)
            {
                luxThetaList.Initialize();//-------------------------------------------

                threadContBSDFMeas = new Thread(contBSDFMeasure);
                contBSDFIsMeas = true;
                unableButton();
                threadContBSDFMeas.Start();
            }

            //双波段BSDF连续测量------------------------------------------------------------------------------------------------------------------------------------
            else if (measProject == 10)
            {
                luxThetaList.Initialize();//-------------------------------------------
                luxThetaList_1.Initialize();//-------------------------------------------

                threadContBSDFMeas = new Thread(contBSDFMeasure);
                contBSDFIsMeas = true;
                unableButton();
                threadContBSDFMeas.Start();
            }


        }

        private void tlmStop_Click(object sender, EventArgs e)
        {
            //------------------------------------------------------------------------------------------------------------------------------------
            if (!singRIsMeas && !contRIsMeas && !singLIsMeas && !contLIsMeas && !singDIsMeas && !contDIsMeas&& !singBSDFIsMeas && !contBSDFIsMeas)//添加BSDF测量判断------------
            {
                MessageBox.Show("未进行测量！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (singRIsMeas)
            {
                if (threadSingRMeas != null)
                {
                    if (threadSingRMeas.IsAlive)
                    {
                        threadSingRMeas.Abort();
                        singRIsMeas = false;
                    }
                }

                BeginInvoke(new dlgLabelDisplay(labelDisplay), "停止测量！");
            }

            if (contRIsMeas)
            {
                if (threadContRMeas != null)
                {
                    if (threadContRMeas.IsAlive)
                    {
                        threadContRMeas.Abort();
                        contRIsMeas = false;
                    }
                }
            }

            if (singLIsMeas)
            {
                if (threadSingLMeas != null)
                {
                    if (threadSingLMeas.IsAlive)
                    {
                        threadSingLMeas.Abort();
                        singLIsMeas = false;
                    }
                }

                BeginInvoke(new dlgLabelDisplay(labelDisplay), "停止测量！");
            }

            if (contLIsMeas)
            {
                if (threadContLMeas != null)
                {
                    if (threadContLMeas.IsAlive)
                    {
                        threadContLMeas.Abort();
                        contLIsMeas = false;
                    }
                }

                BeginInvoke(new dlgLabelDisplay(labelDisplay), "停止测量！");
            }
            enableButton();

            if (singDIsMeas)
            {
                if (threadSingDMeas != null)
                {
                    if (threadSingDMeas.IsAlive)
                    {
                        threadSingDMeas.Abort();
                        singDIsMeas = false;
                    }
                }

                BeginInvoke(new dlgLabelDisplay(labelDisplay), "停止测量！");
            }

            if (contDIsMeas)
            {
                if (threadContDMeas != null)
                {
                    if (threadContDMeas.IsAlive)
                    {
                        threadContDMeas.Abort();
                        contDIsMeas = false;
                    }
                }

                BeginInvoke(new dlgLabelDisplay(labelDisplay), "停止测量！");
            }

            //停止BSDF测量------------------------------------------------------------------------------------
            if (singBSDFIsMeas)
            {
                if (threadSingBSDFMeas != null)
                {
                    if (threadSingBSDFMeas.IsAlive)
                    {
                        threadSingBSDFMeas.Abort();
                        singBSDFIsMeas = false;
                    }
                }

                BeginInvoke(new dlgLabelDisplay(labelDisplay), "停止测量！");
            }

            if (contBSDFIsMeas)
            {
                if (threadContBSDFMeas != null)
                {
                    if (threadContBSDFMeas.IsAlive)
                    {
                        threadContBSDFMeas.Abort();
                        contBSDFIsMeas = false;
                    }
                }

                BeginInvoke(new dlgLabelDisplay(labelDisplay), "停止测量！");
            }
            //-----------------------------------------------------------------------------------------------------


            if (sourceIsJoin)
            {
                classSourceMeter.LightOff(sourceMode, sptSource);
            }

            if (ctrlIsJoin)
            {
                ClassCtrlBorad.d1000_immediate_stop(YCH);
                ClassCtrlBorad.d1000_immediate_stop(XCH);
            }
        }

        private void tlmSpectralDark_Click(object sender, EventArgs e)
        {
            if (spectralIsJoin)
            {
                classSpectralMeter.Dark();
            }
            else
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void tlmLuxDark_Click(object sender, EventArgs e)
        {
            if (spectralIsJoin)
            {
                classSpectralMeter.Dark();
            }
            else
            {
                MessageBox.Show("光谱仪未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void tlmRestore_Click(object sender, EventArgs e)
        {
            if (ctrlIsJoin)
            {
                restoreCtrl();
            }
            else
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void tlmDecel_Click(object sender, EventArgs e)
        {
            if (ctrlIsJoin)
            {
                ClassCtrlBorad.d1000_decel_stop(YCH);
                ClassCtrlBorad.d1000_decel_stop(XCH);
                ClassCtrlBorad.d1000_decel_stop(ZCH);
            }
            else
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void tlmSetting_Click(object sender, EventArgs e)
        {
            frmSetting frm_Setting = new frmSetting();

            frm_Setting.GammaSample = gammaSample;
            frm_Setting.GammaRange = gammaRange;
            frm_Setting.CycleSpace = cycleSpace;
            frm_Setting.MeasSpace = measSpace;

            frm_Setting.IsAutoAdjust = param.IsAutoAdjust;
            frm_Setting.IntTime = param.IntTime;
            frm_Setting.STA = param.STA;
            frm_Setting.BCW = param.BCW;

            frm_Setting.LuxPortName = luxPortName;
            frm_Setting.Distance = distance;

            frm_Setting.SourceMode = sourceMode;
            frm_Setting.SourcePortName = sourcePortName;
            frm_Setting.Voltage = voltSet;
            frm_Setting.Current = currSet;
            frm_Setting.PreheatTime = preheatTime;

            frm_Setting.IsMeas = isMeas;

            frm_Setting.ShowDialog();

            if (frm_Setting.DialogResult == DialogResult.OK)
            {
                gammaSample = frm_Setting.GammaSample;
                gammaRange = frm_Setting.GammaRange;
                cycleSpace = frm_Setting.CycleSpace;
                measSpace = frm_Setting.MeasSpace;

                param.IsAutoAdjust = frm_Setting.IsAutoAdjust;
                param.IntTime = frm_Setting.IntTime;
                param.STA = frm_Setting.STA;
                param.BCW = frm_Setting.BCW;

                luxPortName = frm_Setting.LuxPortName;
                distance = frm_Setting.Distance;

                sourceMode = frm_Setting.SourceMode;
                sourcePortName = frm_Setting.SourcePortName;
                voltSet = frm_Setting.Voltage;
                currSet = frm_Setting.Current;
                preheatTime = frm_Setting.PreheatTime;
            }
        }

        private void tlmRSecCalibration_Click(object sender, EventArgs e)
        {
            frmRSecCalibration frm_rSecCalibration = new frmRSecCalibration();

            frm_rSecCalibration.Class_SourceMeter = classSourceMeter;
            frm_rSecCalibration.SourceMode = sourceMode;
            frm_rSecCalibration.SPTSource = sptSource;
            frm_rSecCalibration.SourceIsJoin = sourceIsJoin;
            frm_rSecCalibration.VoltSet = voltSet;
            frm_rSecCalibration.CurrSet = currSet;

            frm_rSecCalibration.Class_SpectralMeter = classSpectralMeter;
            frm_rSecCalibration.SpectralIsJoin = spectralIsJoin;
            frm_rSecCalibration.Param = param;
            frm_rSecCalibration.Result = result;

            frm_rSecCalibration.SecondParam = secondParam;

            frm_rSecCalibration.ShowDialog();

            if (frm_rSecCalibration.DialogResult == DialogResult.OK)
            {
                secondParam = frm_rSecCalibration.SecondParam;
            }
        }

        private void tlmLSecCalibration_Click(object sender, EventArgs e)
        {
            frmLSecCalibration frm_lSecCalibration = new frmLSecCalibration();
            frm_lSecCalibration.Class_SourceMeter = classSourceMeter;
            frm_lSecCalibration.SourceMode = sourceMode;
            frm_lSecCalibration.SPTSource = sptSource;
            frm_lSecCalibration.SourceIsJoin = sourceIsJoin;
            frm_lSecCalibration.PreheatTime = preheatTime;
            frm_lSecCalibration.VoltSet = voltSet;
            frm_lSecCalibration.CurrSet = currSet;

            frm_lSecCalibration.Class_SpectralMeter = classSpectralMeter;
            frm_lSecCalibration.SpectralIsJoin = spectralIsJoin;
            frm_lSecCalibration.Param = param;
            frm_lSecCalibration.Result = result;

            frm_lSecCalibration.Class_LuxBoard = classLuxBoard;
            frm_lSecCalibration.LuxIsJoin = luxIsJoin;
            frm_lSecCalibration.Distance = distance;
            frm_lSecCalibration.LuxDarkValue = luxDarkValue;

            frm_lSecCalibration.LuxParam = luxParam;

            frm_lSecCalibration.ShowDialog();

            if (frm_lSecCalibration.DialogResult == DialogResult.OK)
            {
                luxParam = frm_lSecCalibration.LuxParam;
            }
        }

        private void tlmManual_Click(object sender, EventArgs e)
        {
            frmManual frm_manual = new frmManual();

            frm_manual.Class_SourceMeter = classSourceMeter;
            frm_manual.SourceMode = sourceMode;
            frm_manual.SPTSource = sptSource;
            frm_manual.SourceIsJoin = sourceIsJoin;
            frm_manual.VoltSet = voltSet;
            frm_manual.CurrSet = currSet;

            frm_manual.CtrlBoardIsJoin = ctrlIsJoin;
            frm_manual.StrVelXCH = strVelXCH;
            frm_manual.MaxVelXCH = maxVelXCH;
            frm_manual.StrVelYCH = strVelYCH;
            frm_manual.MaxVelYCH = maxVelYCH;
            frm_manual.StrVelZCH = strVelZCH;
            frm_manual.MaxVelZCH = maxVelZCH;
            frm_manual.Tacc = tacc;
            frm_manual.ImpulseXCH = impulseXCH;
            frm_manual.ImpulseYCH = impulseYCH;
            frm_manual.ImpulseZCH = impulseZCH;

            frm_manual.Class_spectralMeter = classSpectralMeter;
            frm_manual.SpectralIsJoin = spectralIsJoin;
            frm_manual.Param = param;
            frm_manual.Result = result;

            frm_manual.Class_LuxBoard = classLuxBoard;
            frm_manual.LuxIsJoin = luxIsJoin;
            frm_manual.Distance = distance;
            frm_manual.LuxDarkValue = luxDarkValue;

            frm_manual.SecondParam = secondParam;
            frm_manual.LuxParam = luxParam;

            frm_manual.ShowDialog();
        }

        private void tlmLocation_Click(object sender, EventArgs e)
        {
            frmLocation frm_location = new frmLocation();

            frm_location.CtrlIsJoin = ctrlIsJoin;
            frm_location.StrVelXCH = strVelXCH;
            frm_location.MaxVelXCH = maxVelXCH;
            frm_location.StrVelYCH = strVelYCH;
            frm_location.MaxVelYCH = maxVelYCH;
            frm_location.StrVelZCH = strVelZCH;
            frm_location.MaxVelZCH = maxVelZCH;
            frm_location.Tacc = tacc;
            frm_location.OffsetXCH = offsetXCH;
            frm_location.OffsetYCH = offsetYCH;
            frm_location.OffsetZCH = offsetZCH;
            frm_location.ImpulseXCH = impulseXCH;
            frm_location.ImpulseYCH = impulseYCH;
            frm_location.ImpulseZCH = impulseZCH;

            frm_location.ShowDialog();

            if (frm_location.DialogResult == DialogResult.OK)
            {
                ClassAppConfigure classAppConfigure = new ClassAppConfigure(Application.StartupPath + "\\data\\ini.xml");

                maxVelXCH = classAppConfigure.MaxVelXCH;
                maxVelYCH = classAppConfigure.MaxVelYCH;
                maxVelZCH = classAppConfigure.MaxVelZCH;
                offsetXCH = classAppConfigure.OffsetXCH;
                offsetYCH = classAppConfigure.OffsetYCH;
                offsetZCH = classAppConfigure.OffsetZCH;
            }
        }

        private void tlmSDCM_Click(object sender, EventArgs e)
        {
            frmSDCM frm_SDCM = new frmSDCM();

            frm_SDCM.Index = sdcmIndex;
            frm_SDCM.Lo = lo;
            frm_SDCM.Hi = hi;

            frm_SDCM.ShowDialog();

            if (frm_SDCM.DialogResult == DialogResult.OK)
            {
                sdcmIndex = frm_SDCM.Index;
                lo = frm_SDCM.Lo;
                hi = frm_SDCM.Hi;

                if (measProject == 0)
                {
                    frm_R3.I_indexSDCM = sdcmIndex;
                    frm_R3.RankLo = lo;
                    frm_R3.RankHi = hi;
                    frm_R3.SelectSDCM();
                }
                else if (measProject == 6)
                {
                    frm_R7.SDCMIndex = sdcmIndex;
                    frm_R7.RankLo = lo;
                    frm_R7.RankHi = hi;
                    frm_R7.SelectSDCM();
                }
            }
        }

        private void tlmInformation_Click(object sender, EventArgs e)
        {
            frmInformation frm_information = new frmInformation();

            frm_information.Information = information;

            frm_information.ShowDialog();

            if (frm_information.DialogResult == DialogResult.OK)
            {
                information = frm_information.Information;
            }
        }

        private void tlmMode_Click(object sender, EventArgs e)
        {
            frmMode frm_mode = new frmMode();

            frm_mode.Code = code;

            frm_mode.ShowDialog();

            if (frm_mode.DialogResult == DialogResult.OK)
            {
                isAdmin = frm_mode.IsAdmin;
                isFmsAdmin = frm_mode.IsSuperAdmin;
            }

            if (isAdmin || isFmsAdmin)
            {
                toolStripMenuItem11.Visible = true;
            }
        }

        private void tlmChangeCode_Click(object sender, EventArgs e)
        {
            frmChangeCode frm_changeCode = new frmChangeCode();

            frm_changeCode.Code = code;

            frm_changeCode.ShowDialog();

            if (frm_changeCode.DialogResult == DialogResult.OK)
            {
                code = frm_changeCode.Code;
            }
        }

        private void tlmToolStrip_CheckedChanged(object sender, EventArgs e)
        {
            if (tlmToolStrip.Checked)
            {
                tsrTester.Visible = true;
            }
            else
            {
                tsrTester.Visible = false;
            }
        }

        private void tlmStatusStrip_CheckedChanged(object sender, EventArgs e)
        {
            if (tlmStatusStrip.Checked)
            {
                ssrTester.Visible = true;
            }
            else
            {
                ssrTester.Visible = false;
            }
        }

        private void tlmPolar_CheckedChanged(object sender, EventArgs e)
        {
            if (tlmPolar.Checked)
            {
                if (measProject == 0)
                {
                    frm_R1.Visible = true;
                }
                else if (measProject == 1)
                {
                    frm_L1.Visible = true;
                }
            }
            else
            {
                if (measProject == 0)
                {
                    frm_R1.Visible = false;
                }
                else if (measProject == 1)
                {
                    frm_L1.Visible = false;
                }
            }
        }

        private void tlm2D_CheckedChanged(object sender, EventArgs e)
        {
            if (tlm2D.Checked)
            {
                if (measProject == 0)
                {
                    frm_R2.Visible = true;
                }
                else if (measProject == 1)
                {
                    frm_L2.Visible = true;
                }
            }
            else
            {
                if (measProject == 0)
                {
                    frm_R2.Visible = false;
                }
                else if (measProject == 1)
                {
                    frm_L2.Visible = false;
                }
            }
        }

        private void tlmCIE_CheckedChanged(object sender, EventArgs e)
        {
            if (tlmCIE.Checked)
            {
                if (measProject == 0)
                {
                    frm_R3.Visible = true;
                }
            }
            else
            {
                if (measProject == 0)
                {
                    frm_R3.Visible = false;
                }
            }
        }

        private void tlm3D_CheckedChanged(object sender, EventArgs e)
        {
            if (tlm3D.Checked)
            {
                if (measProject == 1)
                {
                    frm_L3.Visible = true;
                }
            }
            else
            {
                if (measProject == 1)
                {
                    frm_L3.Visible = false;
                }
            }
        }

        private void tlmList_CheckedChanged(object sender, EventArgs e)
        {
            if (tlmList.Checked)
            {
                if (measProject == 0)
                {
                    frm_R4.Visible = true;
                }
                else if (measProject == 1)
                {
                    frm_L4.Visible = true;
                }
            }
            else
            {
                if (measProject == 0)
                {
                    frm_R4.Visible = false;
                }
                else if (measProject == 1)
                {
                    frm_L4.Visible = false;
                }
            }
        }

        private void tlmTileVertical_Click(object sender, EventArgs e)
        {
            if (measProject == 0 && frm_R1 != null)
            {
                frm_R4.Select();
                frm_R2.Select();
                frm_R3.Select();
                frm_R1.Select();
            }
            else if (measProject == 1 && frm_L1 != null)
            {
                frm_L4.Select();
                frm_L2.Select();
                frm_L3.Select();
                frm_L1.Select();
            }

            layoutIndex = 0;

            if (layoutIndex == 0)
            {
                this.LayoutMdi(MdiLayout.TileVertical);
            }
            else if (layoutIndex == 1)
            {
                this.LayoutMdi(MdiLayout.Cascade);
            }
            else if (layoutIndex == 2)
            {
                this.LayoutMdi(MdiLayout.TileHorizontal);
            }
        }

        private void tlmCascade_Click(object sender, EventArgs e)
        {
            if (measProject == 0 && frm_R1 != null)
            {
                frm_R4.Select();
                frm_R2.Select();
                frm_R3.Select();
                frm_R1.Select();
            }
            else if (measProject == 1 && frm_L1 != null)
            {
                frm_L4.Select();
                frm_L2.Select();
                frm_L3.Select();
                frm_L1.Select();
            }

            layoutIndex = 1;

            if (layoutIndex == 0)
            {
                this.LayoutMdi(MdiLayout.TileVertical);
            }
            else if (layoutIndex == 1)
            {
                this.LayoutMdi(MdiLayout.Cascade);
            }
            else if (layoutIndex == 2)
            {
                this.LayoutMdi(MdiLayout.TileHorizontal);
            }
        }

        private void tlmTileHorizontal_Click(object sender, EventArgs e)
        {
            if (measProject == 0 && frm_R1 != null)
            {
                frm_R4.Select();
                frm_R2.Select();
                frm_R3.Select();
                frm_R1.Select();
            }
            else if (measProject == 1 && frm_L1 != null)
            {
                frm_L4.Select();
                frm_L2.Select();
                frm_L3.Select();
                frm_L1.Select();
            }

            layoutIndex = 2;

            if (layoutIndex == 0)
            {
                this.LayoutMdi(MdiLayout.TileVertical);
            }
            else if (layoutIndex == 1)
            {
                this.LayoutMdi(MdiLayout.Cascade);
            }
            else if (layoutIndex == 2)
            {
                this.LayoutMdi(MdiLayout.TileHorizontal);
            }
        }

        private void tlmAbout_Click(object sender, EventArgs e)
        {
            frmAbout frm_about = new frmAbout();

            frm_about.ShowDialog();
        }

        private void tlClear_Click(object sender, EventArgs e)
        {
            if (measProject == 0 && frm_R1 != null)
            {
                frm_R4.Clear();
                frm_R2.Clear();
                frm_R3.Clear();
                frm_R1.Clear();
            }
            else if (measProject == 1 && frm_L1 != null)
            {
                frm_L4.Clear();
                frm_L2.Clear();
                frm_L3.Clear();
                frm_L1.Clear();
            }
            else if (measProject == 2 && frm_L2 != null)
            {
                frm_L4.Clear();
                frm_L2.Clear();
                frm_R4.Clear();
                frm_L2.Clear();
            }
            else if (measProject == 3 && frm_L1 != null)
            {
                frm_L4.Clear();
                frm_L2.Clear();
                frm_L3.Clear();
                frm_L1.Clear();

                //清除BSDF数据-----------------------------------
                luxThetaList = new double[24][];
                //luxList.Clear();
                //-----------------------------------------------
                
            }
            else if (measProject == 10 && frm_L4 != null)
            {
                frm_L4.Clear();
                frm_L2.Clear();
                frm_L4_1.Clear();
                frm_L2_1.Clear();

                //清除BSDF数据-----------------------------------
                luxThetaList = new double[24][];
                luxThetaList_1 = new double[24][];
                //luxList.Clear();
                //luxList_1.Clear();
                //-----------------------------------------------
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (!sourceIsJoin)
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            classSourceMeter.Set(sourceMode, sptSource, voltSet, currSet);
            classSourceMeter.LightOn(sourceMode, sptSource);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (!sourceIsJoin)
            {
                MessageBox.Show("电源未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            classSourceMeter.LightOff(sourceMode, sptSource);
        }

        #endregion

        #region 其它事件

        private void pdcTester_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            #region 色度打印报告

            if (measProject == 0)
            {
                if (colorTempData == null)
                {
                    return;
                }

                ClassPrint classPrint = new ClassPrint();

                Image imgPolar = classPrint.PrintPolar(gammaSample, gammaRange, colorTempData);
                Image img2D = classPrint.PrintRadiant2D(gammaSample, gammaRange, colorTempData);
                Image imgCIE = classPrint.PrintCIE(Application.StartupPath + "\\data\\CIE1931.txt", smallxData, smallyData);
                double midSDCM = frm_R3.CalcSDCM(colorTempData[(int)(colorTempData.Length / 2)], colorTempData[(int)(colorTempData.Length / 2)], sdcmIndex);

                Graphics ghs = e.Graphics;
                Rectangle rtl = new Rectangle();
                PaintEventArgs peg = new PaintEventArgs(ghs, rtl);

                int paperWidth = e.PageBounds.Width;// 打印纸宽度
                int paperHigh = e.PageBounds.Height;// 打印纸高度
                // 12号字体高度
                float spacing = new Font("Times New Roman", 12, FontStyle.Regular).GetHeight(e.Graphics);
                // 行开始位置 三倍字体高度
                float rowStart = 3 * spacing;
                // 列开始位置 三倍字体高度
                float coloumnStart = 3 * spacing;
                // 行结束位置 纸宽减去三倍字体高度
                float rowEnd = paperWidth - 3 * spacing;
                // 列结束位置 纸高减去三倍字体高度
                float coloumnEnd = paperHigh - 3 * spacing;
                // 打印区宽度
                float width = rowEnd - rowStart;
                // 打印区高度
                float height = coloumnEnd - coloumnStart;
                // 列位移
                float coloumnGo = coloumnStart;
                // 公司名称
                Font textFont = new Font("宋体", 12f, FontStyle.Regular);
                Brush brushCoord = Brushes.Black;
                ghs.DrawString("光源色度空间分布测量报告", textFont, brushCoord, rowStart, coloumnGo);// 标题
                ghs.DrawString("佛山市多谱光电科技有限公司", textFont, brushCoord, rowEnd - 220, coloumnGo);
                // 分割线
                coloumnGo += 18;
                Pen linePen = new Pen(Color.Black) { Width = 2f };
                ghs.DrawLine(linePen, rowStart, coloumnGo, rowEnd, coloumnGo);
                // 测试条件标题
                coloumnGo += 12;
                textFont = new Font("宋体", 12f, FontStyle.Bold);
                brushCoord = Brushes.Black;
                ghs.DrawString("测量条件", textFont, brushCoord, rowStart, coloumnGo);
                // 分割线
                coloumnGo += 18;
                linePen = new Pen(Color.Black) { Width = 1f };
                ghs.DrawLine(linePen, rowStart, coloumnGo, rowEnd / 3, coloumnGo);
                // 测试条件参数
                coloumnGo += 12;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("光谱范围：380 - 780nm", textFont, brushCoord, rowStart, coloumnGo);
                coloumnGo += 30;
                ghs.DrawString("角度范围：" + "±" + gammaRange.ToString() + "°", textFont, brushCoord, rowStart, coloumnGo);
                coloumnGo += 30;
                ghs.DrawString("角度采样：" + gammaSample.ToString() + "°", textFont, brushCoord, rowStart, coloumnGo);
                coloumnGo += 30;
                ghs.DrawString("测试角度：" + thetaDegree.ToString() + "°", textFont, brushCoord, rowStart, coloumnGo);
                // 2D Polar
                coloumnGo += 48;
                ghs.DrawImage(imgPolar, rowStart + 5, coloumnGo, (width - 15) / 2, ((width - 15) / 10) * 3f - 20);
                ghs.DrawImage(img2D, rowStart + (width - 15) / 2 + 10, coloumnGo - 10, (width - 15) / 2, ((width - 15) / 10) * 3f);
                coloumnGo += ((width - 15) / 10) * 3f;
                coloumnGo += 48;
                textFont = new Font("宋体", 12f, FontStyle.Bold);
                brushCoord = Brushes.Black;
                ghs.DrawString("测试结果", textFont, brushCoord, rowStart, coloumnGo);
                // 上分割线
                coloumnGo += 18;
                linePen = new Pen(Color.Black) { Width = 1f };
                ghs.DrawLine(linePen, rowStart, coloumnGo, rowEnd, coloumnGo);
                // 竖分割线
                linePen = new Pen(Color.Black) { Width = 1f };
                ghs.DrawLine(linePen, rowStart + width / 2, coloumnGo, rowStart + width / 2, coloumnGo + (width / 2) - 36);
                // 下分割线
                linePen = new Pen(Color.Black) { Width = 1f };
                ghs.DrawLine(linePen, rowStart, coloumnGo + (width / 2) - 36, rowEnd, coloumnGo + (width / 2) - 36);
                // 色度参数标题
                coloumnGo += 12;
                textFont = new Font("宋体", 12f, FontStyle.Bold);
                brushCoord = Brushes.Black;
                ghs.DrawString("色度参数", textFont, brushCoord, rowStart, coloumnGo);
                // CIE
                ghs.DrawImage(imgCIE, rowStart + width / 2 + 30, coloumnGo, (width / 2 - 60), (width / 2 - 60));
                // 色度参数结果
                coloumnGo += 30;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("最大色温：" + colorTempData.Max().ToString("#0.0") + "K", textFont, brushCoord, rowStart, coloumnGo);
                coloumnGo += 30;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("最小色温：" + colorTempData.Min().ToString("#0.0") + "K", textFont, brushCoord, rowStart, coloumnGo);
                coloumnGo += 30;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("中心点色容差：" + midSDCM.ToString(), textFont, brushCoord, rowStart, coloumnGo);
                // 电学参数标题
                coloumnGo += 30;
                textFont = new Font("宋体", 12f, FontStyle.Bold);
                brushCoord = Brushes.Black;
                ghs.DrawString("电学参数", textFont, brushCoord, rowStart, coloumnGo);
                // 电学参数结果
                coloumnGo += 30;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("电压：" + voltCheck.ToString("#0.0000") + "V", textFont, brushCoord, rowStart, coloumnGo);
                coloumnGo += 30;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("电流：" + currCheck.ToString("#0.0000") + "A", textFont, brushCoord, rowStart, coloumnGo);
                coloumnGo += 30;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("电功率：" + (voltCheck * currCheck).ToString("#0.0000") + "W", textFont, brushCoord, rowStart, coloumnGo);
                // 分割线
                coloumnGo = coloumnEnd - 4f * spacing;
                linePen = new Pen(Color.Black) { Width = 2f };
                ghs.DrawLine(linePen, rowStart, coloumnGo, rowEnd, coloumnGo);
                // 测试信息
                coloumnGo += 0.5f * spacing;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("产品类型：" + information[0], textFont, brushCoord, rowStart, coloumnGo);
                ghs.DrawString("制造厂商：" + information[1], textFont, brushCoord, rowStart + width / 3, coloumnGo);
                ghs.DrawString("日    期：" + information[2], textFont, brushCoord, rowStart + 2 * width / 3, coloumnGo);
                coloumnGo += spacing * 1.2f;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("环境温度：" + information[3] + "℃", textFont, brushCoord, rowStart, coloumnGo);
                ghs.DrawString("环境湿度：" + information[4] + "%", textFont, brushCoord, rowStart + width / 3, coloumnGo);
                ghs.DrawString("测试人员：" + information[5], textFont, brushCoord, rowStart + 2 * width / 3, coloumnGo);
            }

            #endregion

            #region 光强打印报告

            else if (measProject == 1)
            {
                if (luxData == null)
                {
                    return;
                }

                ClassPrint classPrint = new ClassPrint();

                Image imgPolar = classPrint.PrintPolar(gammaSample, gammaRange, luxData);
                Image img2D = classPrint.PrintLuminous2D(gammaSample, gammaRange, luxData);
                double halfWidth = classLuxBoard.CalcHalfWidth(luxData, gammaSample);

                Graphics ghs = e.Graphics;
                Rectangle rtl = new Rectangle();
                PaintEventArgs peg = new PaintEventArgs(ghs, rtl);

                int paperWidth = e.PageBounds.Width;// 打印纸宽度
                int paperHigh = e.PageBounds.Height;// 打印纸高度
                // 12号字体高度
                float spacing = new Font("Times New Roman", 12, FontStyle.Regular).GetHeight(e.Graphics);
                // 行开始位置 纸宽增加三倍字体高度
                float rowStart = 3 * spacing;
                // 列开始位置 纸高增加三倍字体高度
                float coloumnStart = 3 * spacing;
                // 行结束位置 纸宽减去三倍字体高度
                float rowEnd = paperWidth - 3 * spacing;
                // 列结束位置 纸高减去三倍字体高度
                float coloumnEnd = paperHigh - 3 * spacing;
                // 打印区宽度
                float width = rowEnd - rowStart;
                // 打印区高度
                float height = coloumnEnd - coloumnStart;
                // 列位移
                float coloumnGo = coloumnStart;
                // 公司名称
                Font textFont = new Font("宋体", 12f, FontStyle.Regular);
                Brush brushCoord = Brushes.Black;
                ghs.DrawString("光源光强空间分布测量报告", textFont, brushCoord, rowStart, coloumnGo);// 标题
                ghs.DrawString("佛山市多谱光电科技有限公司", textFont, brushCoord, rowEnd - 220, coloumnGo);
                // 分割线
                coloumnGo += 18;
                Pen linePen = new Pen(Color.Black) { Width = 2f };
                ghs.DrawLine(linePen, rowStart, coloumnGo, rowEnd, coloumnGo);
                // 测试条件标题
                coloumnGo += 12;
                textFont = new Font("宋体", 12f, FontStyle.Bold);
                brushCoord = Brushes.Black;
                ghs.DrawString("测量条件", textFont, brushCoord, rowStart, coloumnGo);
                // 分割线
                coloumnGo += 18;
                linePen = new Pen(Color.Black) { Width = 1f };
                ghs.DrawLine(linePen, rowStart, coloumnGo, rowEnd / 3, coloumnGo);
                // 测试条件参数
                coloumnGo += 12;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("光谱范围：380 - 780nm", textFont, brushCoord, rowStart, coloumnGo);
                coloumnGo += 30;
                ghs.DrawString("角度范围：" + "±" + gammaRange.ToString() + "°", textFont, brushCoord, rowStart, coloumnGo);
                coloumnGo += 30;
                ghs.DrawString("角度采样：" + gammaSample.ToString() + "°", textFont, brushCoord, rowStart, coloumnGo);
                coloumnGo += 30;
                ghs.DrawString("测试角度：" + thetaDegree.ToString() + "°", textFont, brushCoord, rowStart, coloumnGo);
                // 2D Polar
                coloumnGo += 48;
                ghs.DrawImage(imgPolar, rowStart + 5, coloumnGo, (width - 15) / 2, ((width - 15) / 10) * 3f - 20);
                ghs.DrawImage(img2D, rowStart + (width - 15) / 2 + 10, coloumnGo - 10, (width - 15) / 2, ((width - 15) / 10) * 3f);
                // 测试结果标题
                coloumnGo += ((width - 15) / 10) * 3f;
                coloumnGo += 48;
                textFont = new Font("宋体", 12f, FontStyle.Bold);
                brushCoord = Brushes.Black;
                ghs.DrawString("测试结果", textFont, brushCoord, rowStart, coloumnGo);
                // 分割线
                coloumnGo += 18;
                linePen = new Pen(Color.Black) { Width = 1f };
                ghs.DrawLine(linePen, rowStart, coloumnGo, rowEnd, coloumnGo);
                // 光度参数标题
                coloumnGo += 12;
                ghs.DrawString("光度参数：", textFont, brushCoord, rowStart, coloumnGo);
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                // 光度参数结果
                coloumnGo += 30;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("中心光强：" + luxData[(luxData.Length - 1) / 2].ToString("#0.00") + "cd", textFont, brushCoord, rowStart, coloumnGo);
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("平均光强：" + luxData.Average().ToString("#0.00") + "cd", textFont, brushCoord, rowStart + width / 3, coloumnGo);
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("平均光强发散角：" + halfWidth.ToString() + "°", textFont, brushCoord, rowStart + width / 3 * 2, coloumnGo);
                coloumnGo += 30;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("最大光强：" + luxData.Max().ToString("#0.00") + "cd", textFont, brushCoord, rowStart, coloumnGo);
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("最小光强：" + luxData.Min().ToString("#0.00") + "cd", textFont, brushCoord, rowStart + width / 3, coloumnGo);
                // 电学参数标题
                coloumnGo += 30;
                textFont = new Font("宋体", 12f, FontStyle.Bold);
                brushCoord = Brushes.Black;
                ghs.DrawString("电学参数", textFont, brushCoord, rowStart, coloumnGo);
                // 电学参数结果
                coloumnGo += 30;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("工作电压：" + voltCheck.ToString("#0.0000") + "V", textFont, brushCoord, rowStart, coloumnGo);
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("工作电流：" + currCheck.ToString("#0.0000") + "A", textFont, brushCoord, rowStart + width / 3, coloumnGo);
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("电功率：" + (voltCheck * currCheck).ToString("#0.0000") + "W", textFont, brushCoord, rowStart + width / 3 * 2, coloumnGo);
                // 分割线
                coloumnGo = coloumnEnd - 4f * spacing;
                linePen = new Pen(Color.Black) { Width = 2f };
                ghs.DrawLine(linePen, rowStart, coloumnGo, rowEnd, coloumnGo);
                // 测试信息
                coloumnGo += 0.5f * spacing;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("产品类型：" + information[0], textFont, brushCoord, rowStart, coloumnGo);
                ghs.DrawString("制造厂商：" + information[1], textFont, brushCoord, rowStart + width / 3, coloumnGo);
                ghs.DrawString("日    期：" + information[2], textFont, brushCoord, rowStart + 2 * width / 3, coloumnGo);
                coloumnGo += spacing * 1.2f;
                textFont = new Font("宋体", 12f, FontStyle.Regular);
                ghs.DrawString("环境温度：" + information[3] + "℃", textFont, brushCoord, rowStart, coloumnGo);
                ghs.DrawString("环境湿度：" + information[4] + "%", textFont, brushCoord, rowStart + width / 3, coloumnGo);
                ghs.DrawString("测试人员：" + information[5], textFont, brushCoord, rowStart + 2 * width / 3, coloumnGo);
            }

            #endregion
        }

        #endregion

        private void ssrTester_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void tlSave_ButtonClick(object sender, EventArgs e)
        {
            if (measProject == 3 || measProject == 10)
            {
                tlmSaveXLS.PerformClick();
                tlmSaveCandela.PerformClick();
            }

        }


    }
}
